-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path32-II.go
47 lines (43 loc) · 877 Bytes
/
32-II.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
package main
/***
"题目:**从上到下打印二叉树II**
[从上到下打印二叉树II](https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof)
题目描述:
***/
/**
解法一
说明:
**/
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func levelOrder2(root *TreeNode) [][]int {
var res [][]int
if root == nil {
return res
}
var queue []*TreeNode
var index = 0
queue = append(queue, root)
for len(queue) > 0 {
var temp []*TreeNode
res = append(res, []int{})
for i := 0; i < len(queue); i++ {
res[index] = append(res[index], queue[i].Val)
if queue[i].Left != nil {
temp = append(temp, queue[i].Left)
}
if queue[i].Right != nil {
temp = append(temp, queue[i].Right)
}
}
index++
queue = temp
}
return res
}