forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0111.py
More file actions
28 lines (23 loc) · 703 Bytes
/
0111.py
File metadata and controls
28 lines (23 loc) · 703 Bytes
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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def minDepth(self, root: TreeNode) -> int:
if not root:
return 0
ans = 0
queue = collections.deque([root])
while queue:
ans += 1
for i in range(len(queue)):
node = queue.popleft()
if not node.left and not node.right:
return ans
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return -1