-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlevelOrder_test.go
More file actions
51 lines (47 loc) · 846 Bytes
/
Copy pathlevelOrder_test.go
File metadata and controls
51 lines (47 loc) · 846 Bytes
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 leet_code
import (
"testing"
)
func levelOrder(root *TreeNode) [][]int {
if root == nil {
return nil
}
var d = make([][]int, getTreeNodeH(root))
rangeLevelOrder([]*TreeNode{root}, 0, d)
return d
}
func rangeLevelOrder(root []*TreeNode, j int, d [][]int) (data []*TreeNode) {
if len(root) <= 0 {
return
}
dInt := make([]int, len(root))
d[j] = dInt
for i := 0; i < len(root); i++ {
if root[i] == nil {
continue
}
if root[i].Left != nil {
data = append(data, root[i].Left)
}
if root[i].Right != nil {
data = append(data, root[i].Right)
}
d[j][i] = root[i].Val
}
j++
return rangeLevelOrder(data, j, d)
}
func Test_levelOrder(t *testing.T) {
t.Log(levelOrder(&TreeNode{
Val: 1,
Left: &TreeNode{
Val: 2,
},
Right: &TreeNode{
Val: 3,
Right: &TreeNode{
Val: 4,
},
},
}))
}