-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathdeepest-node-in-binary-tree.py
44 lines (36 loc) · 1.01 KB
/
deepest-node-in-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
"""
Given the root to a binary tree, return the deepest node.
"""
class Node:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
def __str__(self):
return "<" + str(self.val) + ">"
def deepest(node):
if node and not node.left and not node.right:
return (node, 1) # Leaf and its depth
if not node.left: # Then the deepest node is on the right subtree
return increment_depth(deepest(node.right))
elif not node.right: # Then the deepest node is on the left subtree
return increment_depth(deepest(node.left))
return increment_depth(
max(deepest(node.left), deepest(node.right),
key=lambda x: x[1]))
def increment_depth(node_depth_tuple):
node, depth = node_depth_tuple
return (node, depth + 1)
if __name__ == "__main__":
tree = Node(4, Node(2, Node(1, Node(0)), Node(3)), Node(5))
"""
4
/ \
2 5
/ \
1 3
/
0
"""
node_depth_tuple = deepest(tree)
print(node_depth_tuple[0], " of depth ", node_depth_tuple[1])