上次编辑到这里,代码来自缓存 点击恢复默认模板
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> flipMatchVoyage(TreeNode* root, vector<int>& voyage) {
}
};
python3 解法, 执行用时: 48 ms, 内存消耗: 15.9 MB, 提交时间: 2023-06-13 09:58:22
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def __init__(self):
self.i = 0
self.can = True
self.ans = []
def flipMatchVoyage(self, root: TreeNode, voyage: List[int]) -> List[int]:
def dfs(root, voyage):
if not self.can or not root:
return
if root.val == voyage[self.i]:
self.i += 1
if root.left and root.left.val == voyage[self.i]:
dfs(root.left,voyage)
dfs(root.right, voyage)
elif root.right and root.right.val == voyage[self.i]:
if root.left:
self.ans.append(root.val)
dfs(root.right, voyage)
dfs(root.left, voyage)
elif root.left or root.right:
self.can = False
else:
self.can = False
dfs(root, voyage)
if not self.can:
return [-1]
return self.ans
python3 解法, 执行用时: 48 ms, 内存消耗: 16 MB, 提交时间: 2023-06-13 09:56:22
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def flipMatchVoyage(self, root: TreeNode, voyage: List[int]) -> List[int]:
self.flipped = []
self.i = 0
def dfs(node: TreeNode) -> None:
if node:
if node.val != voyage[self.i]:
self.flipped = [-1]
return
self.i += 1
if (self.i < len(voyage) and
node.left and node.left.val != voyage[self.i]):
self.flipped.append(node.val)
dfs(node.right)
dfs(node.left)
else:
dfs(node.left)
dfs(node.right)
dfs(root)
if self.flipped and self.flipped[0] == -1:
self.flipped = [-1]
return self.flipped
java 解法, 执行用时: 0 ms, 内存消耗: 40.3 MB, 提交时间: 2023-06-13 09:55:34
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
List<Integer> ans = new ArrayList<>();
int index = 0;
private boolean dfs(TreeNode root, int[] voyage) {
if (root == null) {
return true;
}
int restore = index;
if (root.val != voyage[index++]) {
return false;
}
if (dfs(root.left, voyage) && dfs(root.right, voyage)) {
return true;
}
index = restore + 1;
boolean left = dfs(root.right, voyage);
boolean right = dfs(root.left, voyage);
if (left && right) {
ans.add(root.val);
}
return left && right;
}
public List<Integer> flipMatchVoyage(TreeNode root, int[] voyage) {
return dfs(root, voyage) ? ans : List.of(-1);
}
}
golang 解法, 执行用时: 0 ms, 内存消耗: 2.5 MB, 提交时间: 2023-06-13 09:54:33
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
// 维护个idx指针来和同步二叉树的深度优先搜索,有价值的代码只有翻转的判断。
func flipMatchVoyage(root *TreeNode, voyage []int) (ans []int) {
var idx int
var n int = len(voyage)
var dfs func(root *TreeNode) bool
dfs = func(root *TreeNode) bool {
if root == nil {
return true
}
if root.Val != voyage[idx] {
return false
}
//翻转
if idx + 1 < n && root.Left != nil && root.Left.Val != voyage[idx + 1] && root.Right != nil && root.Right.Val == voyage[idx + 1] {
ans = append(ans, root.Val)
root.Left, root.Right = root.Right, root.Left
}
idx++
return dfs(root.Left) && dfs(root.Right)
}
if dfs(root) {
return ans
}
return []int{-1}
}