diff --git a/leetcode/README.md b/leetcode/README.md index 1df50583..5eefc9d6 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -306,6 +306,7 @@ | 572 | [Subtree of Another Tree](tree/isSubtree.go) | [Easy][] | | 575 | [Distribute Candies](hash-table/distributeCandies.go) | [Easy][] | | 581 | [Shortest Unsorted Continuous Subarray](array/findUnsortedSubarray.go) | [Easy][] | +| 590 | [N-ary Tree Postorder Traversal](tree/postorder.go) | [Easy][] | | 594 | [Longest Harmonious Subsequence](hash-table/findLHS.go) | [Easy][] | | 598 | [Range Addition II](math/maxCount.go) | [Easy][] | | 599 | [Minimum Index Sum of Two Lists](hash-table/findRestaurant.go) | [Easy][] | diff --git a/leetcode/tree/postorder.go b/leetcode/tree/postorder.go new file mode 100644 index 00000000..b5056140 --- /dev/null +++ b/leetcode/tree/postorder.go @@ -0,0 +1,61 @@ +/* https://leetcode.com/problems/n-ary-tree-postorder-traversal/ +Given the root of an n-ary tree, return the postorder traversal of its nodes' values. + +Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples) + +Example 1: + +Input: root = [1,null,3,2,4,null,5,6] +Output: [5,6,3,2,4,1] +Example 2: + +Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14] +Output: [2,6,14,11,7,3,12,8,4,13,9,10,5,1] + +Constraints: + +The number of nodes in the tree is in the range [0, 10^4]. +0 <= Node.val <= 10^4 +The height of the n-ary tree is less than or equal to 1000. +*/ + +package ltree + +type Node struct { + Val int + Children []*Node +} + +func postorder(root *Node) []int { + // var helper func(root *Node, ans *[]int) + // helper = func(root *Node, ans *[]int) { + // if root == nil { + // return + // } + // for _, child := range root.Children { + // helper(child, ans) + // } + // *ans = append(*ans, root.Val) + // } + + // ans := []int{} + // helper(root, &ans) + // return ans + + ans := []int{} + stack := []*Node{root} + for len(stack) != 0 { + cur := stack[len(stack)-1] + ans = append(ans, cur.Val) + stack = stack[:len(stack)-1] + for _, child := range cur.Children { + stack = append(stack, child) + } + } + + for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 { + ans[i], ans[j] = ans[j], ans[i] + } + + return ans +} diff --git a/leetcode/tree/postorder_test.go b/leetcode/tree/postorder_test.go new file mode 100644 index 00000000..e408886b --- /dev/null +++ b/leetcode/tree/postorder_test.go @@ -0,0 +1,34 @@ +package ltree + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_postorder(t *testing.T) { + assert := assert.New(t) + + // [1,null,3,2,4,null,5,6] + root := + &Node{ + Val: 1, + Children: []*Node{&Node{ + Val: 3, + Children: []*Node{&Node{ + Val: 5, + Children: []*Node{}, + }, &Node{ + Val: 6, + Children: []*Node{}, + }}, + }, &Node{ + Val: 2, + Children: []*Node{}, + }, &Node{ + Val: 4, + Children: []*Node{}, + }}, + } + assert.Equal([]int{5, 6, 3, 2, 4, 1}, postorder(root)) +}