forked from hoanhan101/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
min_depth_test.go
119 lines (95 loc) · 2.79 KB
/
min_depth_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
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
Problem:
- Given a binary tree, find the minimum depth, aka the number of nodes along
the shortest path from the root node to the nearest leaf node.
Example:
- Input:
1
2 3
4 5
6 7
8
Output: 3
Approach:
- Similar to level order traversal problem, except we keep track of the minimum
depth at each level
- Return it immediately once we find the leaf node.
Cost:
- O(n) time, O(n) space.
*/
package gtci
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestMinDepth(t *testing.T) {
t1 := &common.TreeNode{Left: nil, Value: 1, Right: nil}
t2 := &common.TreeNode{Left: nil, Value: 1, Right: nil}
t2.Left = &common.TreeNode{Left: nil, Value: 2, Right: nil}
t2.Left.Right = &common.TreeNode{Left: nil, Value: 3, Right: nil}
t3 := &common.TreeNode{Left: nil, Value: 1, Right: nil}
t3.Left = &common.TreeNode{Left: nil, Value: 2, Right: nil}
t3.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}
t4.Left.Left = &common.TreeNode{Left: nil, Value: 4, Right: nil}
t4.Right.Right = &common.TreeNode{Left: nil, Value: 5, 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.Left = &common.TreeNode{Left: nil, Value: 5, Right: nil}
t5.Right.Right = &common.TreeNode{Left: nil, Value: 6, Right: nil}
t5.Left.Left.Left = &common.TreeNode{Left: nil, Value: 7, Right: nil}
t5.Left.Left.Left.Left = &common.TreeNode{Left: nil, Value: 8, Right: nil}
tests := []struct {
in *common.TreeNode
expected int
}{
{t1, 1},
{t2, 3},
{t3, 2},
{t4, 3},
{t5, 3},
}
for _, tt := range tests {
common.Equal(
t,
tt.expected,
minDepth(tt.in),
)
}
}
func minDepth(root *common.TreeNode) int {
if root == nil {
return 0
}
// initialize a linked list with the root.
queue := common.NewQueue()
queue.Push(root)
// track the minimum depth.
minDepth := 0
for queue.Size() > 0 {
// increase the min depth at each level.
minDepth++
levelSize := queue.Size()
for i := 0; i < levelSize; i++ {
// pop the queue and cache that value to its current level.
current := queue.Pop().(*common.TreeNode)
// return the minimum depth if the current node is the leaf node.
if current.Left == nil && current.Right == nil {
return minDepth
}
// push its left child.
if current.Left != nil {
queue.Push(current.Left)
}
// push its right child.
if current.Right != nil {
queue.Push(current.Right)
}
}
}
return minDepth
}