-
Notifications
You must be signed in to change notification settings - Fork 0
/
2. BT.py
61 lines (49 loc) · 1.4 KB
/
2. BT.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
class Solution:
def createTree(self, root, l):
n = len(l)
def dfs(root, idx):
left = 2 * idx + 1
right = 2 * idx + 2
if left < n:
root.left = Node(l[left])
dfs(root.left, left)
if right < n:
root.right = Node(l[right])
dfs(root.right, right)
temp = root
dfs(temp, 0)
class Node:
def __init__(self, val):
self.right: Node | None = None
self.data = val
self.left: Node | None = None
def traverseInorder(temp, inorder):
if (temp != None):
traverseInorder(temp.left, inorder)
inorder.append(temp.data)
traverseInorder(temp.right, inorder)
return
if __name__ == "__main__":
t = int(input())
for _ in range(0, t):
arr = list(map(int, input().split()))
root = Node(arr[0])
root.left = Node(arr[1])
root.right = Node(arr[2])
root.left.left = Node(arr[3])
root.left.right = Node(arr[4])
root.right.left = Node(arr[5])
root.right.right = Node(arr[6])
obj = Solution()
root0 = Node(arr[0])
obj.createTree(root0, arr)
a = []
traverseInorder(root0, a)
b = []
traverseInorder(root, b)
print(a)
print(b)
if (a == b):
print(1)
else:
print(-1)