forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_080.py
48 lines (33 loc) · 933 Bytes
/
problem_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
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def __repr__(self):
return str(self.val)
def find_deepest_node(root, depth):
if not root.left and not root.right:
return (root, depth)
left_depth = depth
right_depth = depth
if root.left:
left_deepest, left_depth = find_deepest_node(root.left, depth + 1)
if root.right:
right_deepest, right_depth = find_deepest_node(root.right, depth + 1)
return (left_deepest, left_depth) if left_depth > right_depth \
else (right_deepest, right_depth)
def find_deepest_node_helper(root):
return find_deepest_node(root, 0)[0]
a = Node('a')
b = Node('b')
c = Node('c')
d = Node('d')
e = Node('e')
f = Node('f')
a.left = b
a.right = c
b.left = d
assert find_deepest_node_helper(a) == d
c.left = e
e.left = f
assert find_deepest_node_helper(a) == f