-
Notifications
You must be signed in to change notification settings - Fork 43
/
minimum-depth-of-binary-tree.py
201 lines (174 loc) · 5.26 KB
/
minimum-depth-of-binary-tree.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"""
111. Minimum Depth of Binary Tree
Easy
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
Note: A leaf is a node with no children.
Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: 2
Example 2:
Input: root = [2,null,3,null,4,null,5,null,6]
Output: 5
Constraints:
The number of nodes in the tree is in the range [0, 105].
-1000 <= Node.val <= 1000
"""
# V0
# IDEA : BFS
# compare with LC 104 : Maximum Depth of Binary Tree
class Solution(object):
def minDepth(self, root):
# edge case
if not root:
return 0
if root and not root.left and not root.right:
return 1
layer = 1
q = [[layer, root]]
res = []
while q:
for i in range(len(q)):
layer, tmp = q.pop(0)
"""
NOTE !!! : via below condition, we get "layer" of " A leaf is a node with no children."
"""
if tmp and not tmp.left and not tmp.right:
res.append(layer)
if tmp.left:
q.append([layer+1, tmp.left])
if tmp.right:
q.append([layer+1, tmp.right])
# get min
#print ("res = " + str(res))
return min(res)
# V0'
# IDEA : DFS
# compare with LC 104 : Maximum Depth of Binary Tree
class Solution(object):
def minDepth(self, root):
if not root:
return 0
### NOTE here : we need min depth, so if not root.left, then we need to return directly
if not root.left:
return 1 + self.minDepth(root.right)
### NOTE here : we need min depth, so if not root.right, then we need to return directly
elif not root.right:
return 1 + self.minDepth(root.left)
else:
return 1 + min(self.minDepth(root.left), self.minDepth(root.right))
# V0''
# IDEA : BFS
class Solution(object):
def minDepth(self, root):
if not root:
return 0
q = [[root, 1]]
while q:
for i in range(len(q)):
cur, step = q.pop(0)
### NOTE this
if not cur.left and not cur.right:
return step
if cur.left:
q.append([cur.left, step + 1])
if cur.right:
q.append([cur.right, step + 1])
# V1
# https://blog.csdn.net/coder_orz/article/details/51337522
# IDEA : DFS
class Solution(object):
def minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root == None:
return 0
if not root.left:
return 1 + self.minDepth(root.right)
elif not root.right:
return 1 + self.minDepth(root.left)
else:
return 1 + min(self.minDepth(root.left), self.minDepth(root.right))
# V1
# http://bookshadow.com/weblog/2015/11/28/leetcode-minimum-depth-binary-tree/
# IDEA : DFS
class Solution(object):
def minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root is None:
return 0
left = self.minDepth(root.left)
right = self.minDepth(root.right)
if left and right:
return min(left, right) + 1
return max(left, right) + 1
# V1'
# http://bookshadow.com/weblog/2015/11/28/leetcode-minimum-depth-binary-tree/
# IDEA : BFS
class Solution(object):
def minDepth(self, root):
if not root:
return 0
q = [[root, 1]]
while q:
for i in range(len(q)):
cur, step = q.pop(0)
if not cur.left and not cur.right:
return step
if cur.left:
q.append([cur.left, step + 1])
if cur.right:
q.append([cur.right, step + 1])
# V1''
# https://www.jiuzhang.com/solution/minimum-depth-of-binary-tree/#tag-highlight-lang-python
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
this.val = val
this.left, this.right = None, None
"""
class Solution:
"""
@param root: The root of binary tree.
@return: An integer
"""
def minDepth(self, root):
# write your code here
return self.find(root)
def find(self, node):
if node is None:
return 0
left, right = 0, 0
if node.left != None:
left = self.find(node.left)
else:
return self.find(node.right) + 1
if node.right != None:
right = self.find(node.right)
else:
return left + 1
return min(left,right) + 1
# V2
# Time: O(n)
# Space: O(h), h is height of binary tree
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
# @param root, a tree node
# @return an integer
def minDepth(self, root):
if root is None:
return 0
if root.left and root.right:
return min(self.minDepth(root.left), self.minDepth(root.right)) + 1
else:
return max(self.minDepth(root.left), self.minDepth(root.right)) + 1