-
Notifications
You must be signed in to change notification settings - Fork 1
/
NODE_TO_ROOT_PATH.PY
86 lines (83 loc) · 2.62 KB
/
NODE_TO_ROOT_PATH.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
# input - BINARY TREE([50,25,12,None,None,37,30,None,None,None,75,62,None,70,None,None,87,None,None]) node=70
# output - [70,62,75,50]
# -----------------------------------------------------------------------------------
# Here input tree tree is given as
# 50
# / \\
# 25 75
# / \ // \
# 12 37 62 87
# / \\
# 30 70
#
class Node:
def __init__(self,data):
self.data=data
self.left=None
self.right=None
# This class represent node and state
class State:
def __init__(self,node,state):
self.node = node
self.state= state
# tree creation function
def build_tree(arr):
stack = []
root = Node(arr[0])
stack.append(State(root,1))
# if none then increase
# if state = 3 pop
# else increment state of top make new node connect then push
i=0
while stack:
top = stack[-1]
if top.state==1:
i+=1
if arr[i]!=None:
nn = Node(arr[i])
top.node.left = nn
stack.append(State(top.node.left,1))
else:
top.node.left=None
top.state+=1
elif top.state==2:
i+=1
if arr[i]!=None:
nn = Node(arr[i])
top.node.right= nn
stack.append(State(top.node.right,1))
else:
top.node.right=None
top.state+=1
else:
stack.pop()
return root
# this function returns level of node
def get_level(tree):
level = 0
if not tree:
return 0
level = 1+get_level(tree.left)
level = 1+get_level(tree.right)
return level
def node_to_root_path(tree,node,arr):
if tree:
if tree.data == node:
arr.append(tree.data)
return True
flag = node_to_root_path(tree.left,node,arr)
if flag:
arr.append(tree.data)
return True
flag = node_to_root_path(tree.right,node,arr)
if flag:
arr.append(tree.data)
return True
return False
if __name__ == '__main__':
arr = [50,25,12,None,None,37,30,None,None,None,75,62,None,70,None,None,87,None,None]
tree = build_tree(arr)
level = get_level(tree)
arr=[]
node_to_root_path(tree,70,arr)
print(arr)