111-minimum-depth-of-binary-tree
111-minimum-depth-of-binary-tree
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
My solution:
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
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def minDepth(self, root):
"""
:type root: Optional[TreeNode]
:rtype: int
"""
if not root:
return 0
depth = 0
queue = deque()
queue.append(root)
while queue:
length = len(queue)
depth += 1
for i in range(length):
cur = queue.popleft()
if not cur.left and not cur.right:
return depth
if cur.left:
queue.append(cur.left)
if cur.right:
queue.append(cur.right)
return depth
本文由作者按照 CC BY 4.0 进行授权