Skip to content

S590 #470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 26, 2024
Merged

S590 #470

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions leetcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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][] |
Expand Down
61 changes: 61 additions & 0 deletions leetcode/tree/postorder.go
Original file line number Diff line number Diff line change
@@ -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
}
34 changes: 34 additions & 0 deletions leetcode/tree/postorder_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
Loading