Skip to content

Commit 7cea580

Browse files
committed
Time: 496 ms (95.15%), Space: 49.4 MB (66.38%) - LeetHub
1 parent 286c4aa commit 7cea580

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def minDepth(self, root: Optional[TreeNode]) -> int:
9+
if not root: return 0
10+
depth = 0
11+
q = deque()
12+
q.append(root)
13+
while q:
14+
depth += 1
15+
for _ in range(len(q)):
16+
item = q.popleft()
17+
if not item.left and not item.right:
18+
return depth
19+
if item.left:
20+
q.append(item.left)
21+
if item.right:
22+
q.append(item.right)
23+
24+
#DFS
25+
'''
26+
if not root: return 0
27+
d, D = map(self.minDepth, (root.left, root.right))
28+
return 1 + (d or D)
29+
'''

0 commit comments

Comments
 (0)