-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
55 lines (42 loc) · 747 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
func main() {
}
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func levelOrderBottom(root *TreeNode) [][]int {
if root == nil {
return nil
}
result := collect(root)
reverse(result)
return result
}
func reverse(s [][]int) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}
func collect(root *TreeNode) [][]int {
var result [][]int
q := []*TreeNode{root}
for len(q) != 0 {
size := len(q)
var temp []int
for i := 0; i < size; i++ {
n := q[0]
q = q[1:]
if n.Left != nil {
q = append(q, n.Left)
}
if n.Right != nil {
q = append(q, n.Right)
}
temp = append(temp, n.Val)
}
result = append(result, temp)
}
return result
}