-
Notifications
You must be signed in to change notification settings - Fork 0
/
n_ary_tree_level_order_traversal.go
60 lines (53 loc) · 1.06 KB
/
n_ary_tree_level_order_traversal.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
56
57
58
59
60
package graph
func LevelOrder(root *Node) [][]int {
result := [][]int{}
q := []*Node{root}
i := 0
ll := 0
nextLl := 0
var current *Node
level := []int{}
for len(q) > 0 {
current = q[0]
q = q[1:]
if i == ll {
ll = nextLl + len(current.Children)
nextLl = ll
level = append(level, current.Val)
cpLevel := make([]int, len(level))
copy(cpLevel, level)
result = append(result, cpLevel)
level = []int{}
} else if i < ll {
level = append(level, current.Val)
nextLl = nextLl + len(current.Children)
}
if current.Children != nil {
q = append(q, current.Children...)
}
i++
}
return result
}
func LevelOrder2(root *Node) [][]int {
res, queue, levelRes, levelCount := [][]int{}, []*Node{root}, []int{}, 1
if root == nil {
return res
}
for len(queue) > 0 {
node := queue[0]
queue = queue[1:]
queue = append(queue, node.Children...)
levelRes = append(levelRes, node.Val)
levelCount--
if levelCount == 0 {
res = append(res, levelRes)
levelRes = []int{}
levelCount = len(queue)
}
}
return res
}
//1
//3 2 4
//5 6