-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathFebruary-16.py
155 lines (121 loc) · 3.13 KB
/
February-16.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#User function Template for python3
'''
class Node:
def __init__(self,val):
self.data = val
self.left = None
self.right = None
'''
class Solution:
def serialize(self, root):
a = []
def s(r):
if not r:
a.append(-1)
return
a.append(r.data)
s(r.left)
s(r.right)
s(root)
return a
def deSerialize(self, arr):
self.i = 0
def d():
if self.i >= len(arr) or arr[self.i] == -1:
self.i += 1
return None
r = Node(arr[self.i])
self.i += 1
r.left = d()
r.right = d()
return r
return d()
#{
# Driver Code Starts
#Initial Template for Python 3
#Initial Template for Python 3
#Contributed by Suman Rana
from collections import deque
# Tree Node
class Node:
def __init__(self, val):
self.right = None
self.data = val
self.left = None
# Function to Build Tree
def buildTree(s):
#Corner Case
if (len(s) == 0 or s[0] == "N"):
return None
# Creating list of strings from input
# string after spliting by space
ip = list(map(str, s.split()))
# Create the root of the tree
root = Node(int(ip[0]))
size = 0
q = deque()
# Push the root to the queue
q.append(root)
size = size + 1
# Starting from the second element
i = 1
while (size > 0 and i < len(ip)):
# Get and remove the front of the queue
currNode = q[0]
q.popleft()
size = size - 1
# Get the current node's value from the string
currVal = ip[i]
# If the left child is not null
if (currVal != "N"):
# Create the left child for the current node
currNode.left = Node(int(currVal))
# Push it to the queue
q.append(currNode.left)
size = size + 1
# For the right child
i = i + 1
if (i >= len(ip)):
break
currVal = ip[i]
# If the right child is not null
if (currVal != "N"):
# Create the right child for the current node
currNode.right = Node(int(currVal))
# Push it to the queue
q.append(currNode.right)
size = size + 1
i = i + 1
return root
def inorder(root):
if not root:
return
inorder(root.left)
print(root.data, end=" ")
inorder(root.right)
def _deleteTree(node):
if (node == None):
return
# first delete both subtrees
_deleteTree(node.left)
_deleteTree(node.right)
node.left = None
node.right = None
# then delete the node
# Deletes a tree and sets the root as NULL
def deleteTree(node_ref):
_deleteTree(node_ref)
node_ref = None
if __name__ == "__main__":
t = int(input())
for _ in range(0, t):
root = buildTree(input())
ob = Solution()
A = ob.serialize(root)
deleteTree(root)
root = None
r = ob.deSerialize(A)
inorder(r)
print()
print("~")
# } Driver Code Ends