-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path146_BufferBox_Prune_BinaryTree.py
executable file
·109 lines (84 loc) · 2.59 KB
/
146_BufferBox_Prune_BinaryTree.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
This question was asked by BufferBox.
Given a binary tree where all nodes are either 0 or 1, prune the tree so that subtrees containing all 0s are removed.
For example, given the following tree:
0
/ \
1 0
/ \
1 0
/ \
0 0
should be pruned to:
0
/ \
1 0
/
1
We do not remove the tree at the root or its left child because it still has a 1 as a descendant.
"""
class Node:
def __init__(self, data, left=None, right=None):
self.data = data
self.left = left
self.right = right
def __repr__(self):
return "{}: ({}, {})".format(self.data,
self.left.data if self.left else None,
self.right.data if self.right else None)
def print_level_wise(root, l=0, nodes=None):
if nodes is None: # set default value
nodes = []
try:
nodes[l].append(root.data)
except:
nodes.append([])
nodes[l].append(root.data)
if root.left:
nodes = print_level_wise(root.left, l+1, nodes)
if root.right:
nodes = print_level_wise(root.right, l + 1, nodes)
return nodes
def prune(root):
def helper(node):
can_rm_node, can_rm_left, can_rm_right = True, True, True
if node.left:
can_rm_left = helper(node.left)
if can_rm_left:
node.left = None
if node.right:
can_rm_right = helper(node.right)
if can_rm_right:
node.right = None
if node.data == 1:
can_rm_node = False
return can_rm_node and can_rm_left and can_rm_right
helper(root)
return root
def prune_redux(root):
if root is None:
return None
root.left, root.right = prune_redux(root.left), prune_redux(root.right)
if root.left is None and root.right is None and root.data ==0:
return None
return root
if __name__ == '__main__':
tree = Node(0,
left=Node(1),
right=Node(0,
left=Node(1, left=Node(0), right=Node(0)),
right=Node(0)))
print(*print_level_wise(tree), sep='\n')
tree = prune(root=tree)
print("---"*3)
print(*print_level_wise(tree), sep='\n')
print("===" * 3)
tree = Node(0,
left=Node(1),
right=Node(0,
left=Node(1, left=Node(0), right=Node(0)),
right=Node(0)))
print(*print_level_wise(tree), sep='\n')
tree = prune_redux(root=tree)
print("---"*3)
print(*print_level_wise(tree), sep='\n')