forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmin_height.py
More file actions
64 lines (50 loc) · 1.69 KB
/
min_height.py
File metadata and controls
64 lines (50 loc) · 1.69 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
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 down to the nearest leaf.
Reference: https://en.wikipedia.org/wiki/Binary_tree
Complexity:
Time: O(n)
Space: O(n)
"""
from __future__ import annotations
from algorithms.tree.tree import TreeNode
def min_depth(self: object, root: TreeNode | None) -> int:
"""Compute the minimum depth of a binary tree using recursion.
Args:
self: Unused parameter (kept for backward compatibility).
root: The root of the binary tree.
Returns:
The minimum depth of the tree.
"""
if root is None:
return 0
if root.left is not None or root.right is not None:
return max(self.minDepth(root.left), self.minDepth(root.right)) + 1
return min(self.minDepth(root.left), self.minDepth(root.right)) + 1
def min_height(root: TreeNode | None) -> int:
"""Compute the minimum depth of a binary tree using iterative BFS.
Args:
root: The root of the binary tree.
Returns:
The minimum depth (number of levels to nearest leaf) of the tree.
Examples:
>>> min_height(None)
0
"""
if root is None:
return 0
height = 0
level: list[TreeNode] = [root]
while level:
height += 1
new_level: list[TreeNode] = []
for node in level:
if node.left is None and node.right is None:
return height
if node.left is not None:
new_level.append(node.left)
if node.right is not None:
new_level.append(node.right)
level = new_level
return height