Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Aug 26, 2024
1 parent 19e912d commit ca3c124
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions leetcode/590.N-aryTreePostorderTraversal/sol.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit ca3c124

Please sign in to comment.