-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsolution.go
51 lines (45 loc) · 891 Bytes
/
solution.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
package leetcode
/*
* @lc app=leetcode.cn id=107 lang=golang
*
* [107] 二叉树的层次遍历 II
*/
// @lc code=start
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func levelOrderBottom(root *TreeNode) [][]int {
var res [][]int
if root == nil {
return res
}
queue := []*TreeNode{root}
for len(queue) > 0 {
l := len(queue)
list := make([]int, 0)
for i := 0; i < l; i++ {
node := queue[i]
list = append(list, node.Val)
if node.Left != nil {
queue = append(queue, node.Left)
}
if node.Right != nil {
queue = append(queue, node.Right)
}
}
res = append([][]int{list}, res...)
queue = queue[l:]
}
return res
}
// @lc code=end