-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathminimum-depth-of-binary-tree.py
49 lines (34 loc) · 1.14 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
from collections import deque
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def minDepth(self, root: TreeNode) -> int:
queue = deque()
if root:
queue.append((root, 1))
while queue:
node, depth = queue.popleft()
children = list(filter(lambda n: n, (node.left, node.right)))
if not children:
return depth
for child in children:
queue.append((child, depth + 1))
return 0
def minDepthDFS(self, root: TreeNode) -> int:
if not root:
return 0
stack = []
stack.append((root, 1))
min_depth = float("+inf")
while stack:
node, depth = stack.pop()
children = list(filter(lambda n: n, (node.left, node.right)))
for child in children:
stack.append((child, depth + 1))
if not children:
min_depth = min(min_depth, depth)
return min_depth