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

posted on 2019-04-27 17:15  Sempron2800+  阅读(82)  评论(0编辑  收藏  举报