-
Notifications
You must be signed in to change notification settings - Fork 892
/
problem_254.py
46 lines (38 loc) · 852 Bytes
/
problem_254.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
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def __repr__(self):
return "{}=[{},{}]".format(self.val, self.left, self.right)
def __hash__(self):
return hash(self.val)
def recover_full(root):
if not root.left and not root.right:
return root
elif root.left and root.right:
root.left = recover_full(root.left)
root.right = recover_full(root.right)
return root
elif root.left:
return recover_full(root.left)
elif root.right:
return recover_full(root.right)
# Tests
a = Node("a")
b = Node("b")
c = Node("c")
d = Node("d")
e = Node("e")
f = Node("f")
g = Node("g")
h = Node("h")
a.left = b
a.right = c
b.left = d
d.right = f
c.right = e
e.left = g
e.right = h
print(a)
print(recover_full(a))