-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path111.py
More file actions
26 lines (24 loc) · 693 Bytes
/
111.py
File metadata and controls
26 lines (24 loc) · 693 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
# 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
res = 1
nodes = [root]
while nodes:
next_nodes = []
for node in nodes:
if not node.left and not node.right:
return res
if node.left:
next_nodes.append(node.left)
if node.right:
next_nodes.append(node.right)
nodes = next_nodes
res += 1
return res