forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepest_left.py
More file actions
54 lines (41 loc) · 1.3 KB
/
deepest_left.py
File metadata and controls
54 lines (41 loc) · 1.3 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
"""
Deepest Left Leaf
Given a binary tree, find the deepest node that is the left child of its
parent node.
Reference: https://en.wikipedia.org/wiki/Binary_tree
Complexity:
Time: O(n)
Space: O(n) due to recursion stack
"""
from __future__ import annotations
from algorithms.tree.tree import TreeNode
class DeepestLeft:
"""Container to track the deepest left node found during traversal.
Examples:
>>> dl = DeepestLeft()
>>> dl.depth
0
"""
def __init__(self) -> None:
self.depth: int = 0
self.Node: TreeNode | None = None
def find_deepest_left(
root: TreeNode | None, is_left: bool, depth: int, res: DeepestLeft
) -> None:
"""Recursively find the deepest left child in a binary tree.
Args:
root: The current node being examined.
is_left: Whether the current node is a left child.
depth: The current depth in the tree.
res: A DeepestLeft instance tracking the best result so far.
Examples:
>>> res = DeepestLeft()
>>> find_deepest_left(None, True, 1, res)
"""
if not root:
return
if is_left and depth > res.depth:
res.depth = depth
res.Node = root
find_deepest_left(root.left, True, depth + 1, res)
find_deepest_left(root.right, False, depth + 1, res)