1 class Solution:
2 def flipMatchVoyage(self, root, voyage):
3 res = []
4 self.i = 0
5 def dfs(root):
6 if not root: return True
7 if root.val != voyage[self.i]: return False
8 self.i += 1
9 if root.left and root.left.val != voyage[self.i]:
10 res.append(root.val)
11 root.left,root.right = root.right, root.left
12 return dfs(root.left) and dfs(root.right)
13 return res if dfs(root) else [-1]
参考:https://leetcode.com/problems/flip-binary-tree-to-match-preorder-traversal/discuss/214216/JavaC%2B%2BPython-DFS-Solution