Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
flip-binary-tree-to-match-preorder-traversal.py 995 Bytes
Copy Edit Raw Blame History
# Time: O(n)
# Space: O(h)
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def flipMatchVoyage(self, root, voyage):
"""
:type root: TreeNode
:type voyage: List[int]
:rtype: List[int]
"""
def dfs(root, voyage, i, result):
if not root:
return True
if root.val != voyage[i[0]]:
return False
i[0] += 1
if root.left and root.left.val != voyage[i[0]]:
result.append(root.val)
return dfs(root.right, voyage, i, result) and \
dfs(root.left, voyage, i, result)
return dfs(root.left, voyage, i, result) and \
dfs(root.right, voyage, i, result)
result = []
return result if dfs(root, voyage, [0], result) else [-1]
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

Search