1
# Runtime: 50 ms (Top 21.2%) | Memory: 16.23 MB (Top 89.3%)5
def flipMatchVoyage(self, root, voyage):7
# ------------------------------12
# base case aka stop condition13
# empty node or empty tree17
if root.val != voyage[dfs.idx]:19
# current node mismatch, no chance to make correction by flip22
# voyage index moves forward25
if root.left and (root.left.val != voyage[dfs.idx]):27
# left child mismatch, flip with right child if right child exists28
root.right and result.append(root.val)30
# check subtree in preorder DFS with child node flip31
return dfs(root.right) and dfs(root.left)35
# left child match, check subtree in preorder DFS36
return dfs(root.left) and dfs(root.right)38
# --------------------------43
# voyage index during dfs46
# start checking from root node49
return result if good else [-1]