-
Notifications
You must be signed in to change notification settings - Fork 376
/
level_order_traversal_test.go
106 lines (84 loc) · 2.55 KB
/
level_order_traversal_test.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Problem:
- Given a binary tree, populate the values of all nodes of each level
from left to right in separate sub-arrays.
Example:
- Input:
1
2 3
4 5
Output: []interface{}{[]interface{}{1}, []interface{}{2, 3}, []interface{}{4, 5}}
Approach:
- Start by pushing the root node to the queue.
- Keep iterating until the queue is empty.
- At each step,
- send the front of the queue and dequeue it
- enqueue its left and right child
Cost:
- O(n) time, O(n) space.
*/
package gtci
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestLevelOrderTraverse(t *testing.T) {
t1 := &common.TreeNode{}
t2 := &common.TreeNode{Left: nil, Value: 1, Right: nil}
t3 := &common.TreeNode{Left: nil, Value: 1, Right: nil}
t3.Left = &common.TreeNode{Left: nil, Value: 2, Right: nil}
t3.Left.Right = &common.TreeNode{Left: nil, Value: 3, Right: nil}
t4 := &common.TreeNode{Left: nil, Value: 1, Right: nil}
t4.Left = &common.TreeNode{Left: nil, Value: 2, Right: nil}
t4.Right = &common.TreeNode{Left: nil, Value: 3, Right: nil}
t5 := &common.TreeNode{Left: nil, Value: 1, Right: nil}
t5.Left = &common.TreeNode{Left: nil, Value: 2, Right: nil}
t5.Right = &common.TreeNode{Left: nil, Value: 3, Right: nil}
t5.Left.Left = &common.TreeNode{Left: nil, Value: 4, Right: nil}
t5.Right.Right = &common.TreeNode{Left: nil, Value: 5, Right: nil}
tests := []struct {
in *common.TreeNode
expected []interface{}
}{
{t1, []interface{}{[]interface{}{0}}},
{t2, []interface{}{[]interface{}{1}}},
{t3, []interface{}{[]interface{}{1}, []interface{}{2}, []interface{}{3}}},
{t4, []interface{}{[]interface{}{1}, []interface{}{2, 3}}},
{t5, []interface{}{[]interface{}{1}, []interface{}{2, 3}, []interface{}{4, 5}}},
}
for _, tt := range tests {
common.Equal(
t,
tt.expected,
levelOrderTraverse(tt.in),
)
}
}
func levelOrderTraverse(root *common.TreeNode) []interface{} {
out := common.NewList()
if root == nil {
return out.Slice()
}
// initialize a queue with the root.
queue := common.NewQueue()
queue.Push(root)
for queue.Size() > 0 {
levelSize := queue.Size()
currentLevel := common.NewList()
for i := 0; i < levelSize; i++ {
// pop the queue and cache that value to its current level.
current := queue.Pop().(*common.TreeNode)
currentLevel.PushBack(current.Value)
// push its left child.
if current.Left != nil {
queue.Push(current.Left)
}
// push its right child.
if current.Right != nil {
queue.Push(current.Right)
}
}
out.PushBack(currentLevel.Slice())
}
return out.Slice()
}