-
Notifications
You must be signed in to change notification settings - Fork 0
/
080.py
60 lines (44 loc) · 1.29 KB
/
080.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
50
51
52
53
54
55
56
57
58
59
60
"""
Problem:
Given the root of a binary tree, return a deepest node. For example, in the following
tree, return d.
a
/ \
b c
/
d
"""
from typing import Optional, Tuple
from DataStructures.Tree import BinaryTree, Node
def deepest_node_helper(node: Node) -> Tuple[int, Optional[Node]]:
if node is None:
return 0, None
if not (node.left and node.right):
return 1, node
# getting the deepest node of the left-subtree
left_height, left_node = 0, None
if node.left:
left_height, left_node = deepest_node_helper(node.left)
# getting the deepest node of the right-subtree
right_height, right_node = 0, None
if node.right:
right_height, right_node = deepest_node_helper(node.right)
# comparing and returning the deepest node
if left_height > right_height:
return left_height + 1, left_node
return right_height + 1, right_node
def deepest_node(tree: BinaryTree) -> Node:
_, node = deepest_node_helper(tree.root)
return node
if __name__ == "__main__":
tree = BinaryTree()
tree.root = Node("a")
tree.root.left = Node("b")
tree.root.right = Node("c")
tree.root.left.left = Node("d")
print(deepest_node(tree))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(log(n)) [recursion depth]
"""