forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0971.py
More file actions
25 lines (21 loc) · 695 Bytes
/
Copy path0971.py
File metadata and controls
25 lines (21 loc) · 695 Bytes
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
class Solution:
def flipMatchVoyage(self, root, voyage):
"""
:type root: TreeNode
:type voyage: List[int]
:rtype: List[int]
"""
res = list()
i = 0
def dfs(root):
nonlocal i
if not root:
return True
if root.val != voyage[i]:
return False
i += 1
if root.left and root.left.val != voyage[i]:
res.append(root.val)
root.left, root.right = root.right, root.left
return dfs(root.left) and dfs(root.right)
return res if dfs(root) else [-1]