forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlip Binary Tree To Match Preorder Traversal.py
59 lines (50 loc) · 2.02 KB
/
Flip Binary Tree To Match Preorder Traversal.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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def traverse(self,node,index,n):
res = []
if not node:
index[0] -= 1
return True,[]
# when we reach the end of voyage but still node is present
if node and index[0] == n : return False,[]
#print('Here node = ',node.val, 'Index = ',index[0] , 'voyage val is',self.voyage[index[0]])
# if node.val is not same as voyage[index] in preorder traverse
if node.val != self.voyage[index[0]] : return False,[]
if not node.left and not node.right : return True , []
# for traversing first check if node.left is voyage[index+1] or node.right or None
old = index[0]
# try left and right
index[0] = index[0] + 1
a,l1 = self.traverse(node.left,index,n)
index[0] = index[0] + 1
b,l2 = self.traverse(node.right,index,n)
#print('node =',node.val,' left1 =',a,' and right1 =',b)
if a and b :
res = []
for i in l1: res.append(i)
for i in l2: res.append(i)
return True , res
#try right and left
index[0] = old
index[0] = index[0] + 1
a,l1 = self.traverse(node.right,index,n)
index[0] = index[0] + 1
b,l2 = self.traverse(node.left,index,n)
#print('node =',node.val,' left2 =',b,' and right2 =',a)
if a and b :
res = []
res.append(node.val)
for i in l1: res.append(i)
for i in l2: res.append(i)
return True , res
return False,[]
def flipMatchVoyage(self, root: Optional[TreeNode], voyage: List[int]) -> List[int]:
self.voyage = voyage
ans , lst = self.traverse(root,[0],len(voyage))
if ans == False : return [-1]
return lst