-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimumDepth.py
55 lines (51 loc) · 1.1 KB
/
minimumDepth.py
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
# Definition for a binary tree node
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
# @param root, a tree node
# @return an integer
def minDepth(root):
if root == None:
return 0
queue = [root]
count = 1
nextCount = 0
depth = 1
while len(queue) > 0:
node = queue.pop(0)
count -= 1
if node.left == None and node.right == None:
return depth
if node.left != None:
queue.append(node.left)
nextCount += 1
if node.right != None:
queue.append(node.right)
nextCount += 1
if count == 0:
depth += 1
count = nextCount
nextCount = 0
a = TreeNode('a')
b = TreeNode('b')
c = TreeNode('c')
a.left = b
a.right = c
assert minDepth(a) == 2
assert minDepth(c) == 1
d = TreeNode('d')
e = TreeNode('e')
d.right = e
assert minDepth(d) == 2
f = TreeNode('f')
g = TreeNode('g')
h = TreeNode('h')
i = TreeNode('i')
j = TreeNode('j')
f.left = g
f.right = h
g.left = i
h.right = j
assert minDepth(f) == 3