From ca3c1243a617a0dc495c440c40ba04506fec86cc Mon Sep 17 00:00:00 2001 From: ductnn Date: Mon, 26 Aug 2024 13:38:32 +0700 Subject: [PATCH] add sol --- .../590.N-aryTreePostorderTraversal/sol.go | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 leetcode/590.N-aryTreePostorderTraversal/sol.go diff --git a/leetcode/590.N-aryTreePostorderTraversal/sol.go b/leetcode/590.N-aryTreePostorderTraversal/sol.go new file mode 100644 index 0000000..1a744b4 --- /dev/null +++ b/leetcode/590.N-aryTreePostorderTraversal/sol.go @@ -0,0 +1,41 @@ +package main + +import "fmt" + +/** + * Definition for a Node. + * type Node struct { + * Val int + * Children []*Node + * } + */ + +type Node struct { + Val int + Children []*Node +} + +func postorder(root *Node) []int { + if root == nil { + return []int{} + } + + res := []int{} + for _, child := range root.Children { + res = append(res, postorder(child)...) + } + res = append(res, root.Val) + + return res +} + +func main() { + root := &Node{Val: 1} + child1 := &Node{Val: 3, Children: []*Node{{Val: 5}, {Val: 6}}} + child2 := &Node{Val: 2} + child3 := &Node{Val: 4} + root.Children = []*Node{child1, child2, child3} + + result := postorder(root) + fmt.Println(result) +}