-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathRemove_Path_Less_Than_K.py
49 lines (36 loc) · 1.02 KB
/
Remove_Path_Less_Than_K.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
# Remove nodes on root to leaf paths of length < K
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def remove_path_less_than(root, k):
return remove_path_util(root, 1, k)
def remove_path_util(root, level, k):
if root is None:
return None
root.left = remove_path_util(root.left, level+1, k)
root.right = remove_path_util(root.right, level+1, k)
if root.left is None and root.right is None and level < k:
del root
return None
return root
def in_order(root):
if root is None:
return
in_order(root.left)
print(root.data, end=' ')
in_order(root.right)
if __name__ == '__main__':
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.left.left.left = Node(7)
root.right.right = Node(6)
root.right.right.left = Node(8)
in_order(root)
print()
root = remove_path_less_than(root, 4)
in_order(root)