forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_036.py
72 lines (53 loc) · 1.33 KB
/
problem_036.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
61
62
63
64
65
66
67
68
69
70
71
72
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def __repr__(self):
return str(self.data)
def find_largest_and_parent(node):
parent = None
while node.right:
parent = node
node = node.right
return node, parent
def find_second_largest(root):
if not root:
return None
second_largest = None
if root.left and not root.right:
second_largest, _ = find_largest_and_parent(root.left)
else:
_, second_largest = find_largest_and_parent(root)
print("second_largest", second_largest)
return second_largest
def test_0():
node_a = Node(5)
assert not find_second_largest(node_a)
def test_1():
node_a = Node(5)
node_b = Node(3)
node_c = Node(8)
node_d = Node(2)
node_e = Node(4)
node_f = Node(7)
node_g = Node(9)
node_a.left = node_b
node_a.right = node_c
node_b.left = node_d
node_b.right = node_e
node_c.left = node_f
node_c.right = node_g
assert find_second_largest(node_a).data == 8
def test_2():
node_a = Node(5)
node_b = Node(3)
node_d = Node(2)
node_e = Node(4)
node_a.left = node_b
node_b.left = node_d
node_b.right = node_e
assert find_second_largest(node_a).data == 4
test_0()
test_1()
test_2()