Skip to content

Commit 0447478

Browse files
authored
feat: add solutions to lc problem: No0971 (doocs#1584)
No.0971.Flip Binary Tree To Match Preorder Traversal
1 parent 28664d9 commit 0447478

File tree

7 files changed

+604
-0
lines changed

7 files changed

+604
-0
lines changed

solution/0900-0999/0971.Flip Binary Tree To Match Preorder Traversal/README.md

+211
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,233 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
**方法一:DFS**
62+
63+
我们可以通过深度优先搜索的方式遍历整棵树,用一个下标 $i$ 记录当前遍历到的节点在数组 $voyage$ 中的下标,如果当前遍历到的节点的值不等于 $voyage[i]$,那么说明翻转后无法匹配,我们标记 $ok$ 为 `false`,并直接返回。否则,我们将 $i$ 的值加 $1$,然后判断当前节点是否有左子节点,如果没有,或者左子节点的值等于 $voyage[i]$,那么我们递归遍历当前的左右子节点;否则,我们需要翻转当前节点,然后再递归遍历当前的右子节点和左子节点。
64+
65+
搜索结束后,如果 $ok$ 为 `true`,那么说明翻转后可以匹配,我们返回答案数组 $ans$,否则返回 $[-1]$。
66+
67+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是树中的节点数目。
68+
6169
<!-- tabs:start -->
6270

6371
### **Python3**
6472

6573
<!-- 这里可写当前语言的特殊实现逻辑 -->
6674

6775
```python
76+
# Definition for a binary tree node.
77+
# class TreeNode:
78+
# def __init__(self, val=0, left=None, right=None):
79+
# self.val = val
80+
# self.left = left
81+
# self.right = right
82+
class Solution:
83+
def flipMatchVoyage(self, root: Optional[TreeNode], voyage: List[int]) -> List[int]:
84+
def dfs(root):
85+
nonlocal i, ok
86+
if root is None or not ok:
87+
return
88+
if root.val != voyage[i]:
89+
ok = False
90+
return
91+
i += 1
92+
if root.left is None or root.left.val == voyage[i]:
93+
dfs(root.left)
94+
dfs(root.right)
95+
else:
96+
ans.append(root.val)
97+
dfs(root.right)
98+
dfs(root.left)
6899

100+
ans = []
101+
i = 0
102+
ok = True
103+
dfs(root)
104+
return ans if ok else [-1]
69105
```
70106

71107
### **Java**
72108

73109
<!-- 这里可写当前语言的特殊实现逻辑 -->
74110

75111
```java
112+
/**
113+
* Definition for a binary tree node.
114+
* public class TreeNode {
115+
* int val;
116+
* TreeNode left;
117+
* TreeNode right;
118+
* TreeNode() {}
119+
* TreeNode(int val) { this.val = val; }
120+
* TreeNode(int val, TreeNode left, TreeNode right) {
121+
* this.val = val;
122+
* this.left = left;
123+
* this.right = right;
124+
* }
125+
* }
126+
*/
127+
class Solution {
128+
private int i;
129+
private boolean ok;
130+
private int[] voyage;
131+
private List<Integer> ans = new ArrayList<>();
132+
133+
public List<Integer> flipMatchVoyage(TreeNode root, int[] voyage) {
134+
this.voyage = voyage;
135+
ok = true;
136+
dfs(root);
137+
return ok ? ans : List.of(-1);
138+
}
139+
140+
private void dfs(TreeNode root) {
141+
if (root == null || !ok) {
142+
return;
143+
}
144+
if (root.val != voyage[i]) {
145+
ok = false;
146+
return;
147+
}
148+
++i;
149+
if (root.left == null || root.left.val == voyage[i]) {
150+
dfs(root.left);
151+
dfs(root.right);
152+
} else {
153+
ans.add(root.val);
154+
dfs(root.right);
155+
dfs(root.left);
156+
}
157+
}
158+
}
159+
```
160+
161+
### **C++**
162+
163+
```cpp
164+
/**
165+
* Definition for a binary tree node.
166+
* struct TreeNode {
167+
* int val;
168+
* TreeNode *left;
169+
* TreeNode *right;
170+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
171+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
172+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
173+
* };
174+
*/
175+
class Solution {
176+
public:
177+
vector<int> flipMatchVoyage(TreeNode* root, vector<int>& voyage) {
178+
bool ok = true;
179+
int i = 0;
180+
vector<int> ans;
181+
function<void(TreeNode*)> dfs = [&](TreeNode* root) {
182+
if (!root || !ok) {
183+
return;
184+
}
185+
if (root->val != voyage[i]) {
186+
ok = false;
187+
return;
188+
}
189+
++i;
190+
if (!root->left || root->left->val == voyage[i]) {
191+
dfs(root->left);
192+
dfs(root->right);
193+
} else {
194+
ans.push_back(root->val);
195+
dfs(root->right);
196+
dfs(root->left);
197+
}
198+
};
199+
dfs(root);
200+
return ok ? ans : vector<int>{-1};
201+
}
202+
};
203+
```
204+
205+
### **Go**
206+
207+
```go
208+
/**
209+
* Definition for a binary tree node.
210+
* type TreeNode struct {
211+
* Val int
212+
* Left *TreeNode
213+
* Right *TreeNode
214+
* }
215+
*/
216+
func flipMatchVoyage(root *TreeNode, voyage []int) []int {
217+
i := 0
218+
ok := true
219+
ans := []int{}
220+
var dfs func(*TreeNode)
221+
dfs = func(root *TreeNode) {
222+
if root == nil || !ok {
223+
return
224+
}
225+
if root.Val != voyage[i] {
226+
ok = false
227+
return
228+
}
229+
i++
230+
if root.Left == nil || root.Left.Val == voyage[i] {
231+
dfs(root.Left)
232+
dfs(root.Right)
233+
} else {
234+
ans = append(ans, root.Val)
235+
dfs(root.Right)
236+
dfs(root.Left)
237+
}
238+
}
239+
dfs(root)
240+
if !ok {
241+
return []int{-1}
242+
}
243+
return ans
244+
}
245+
```
246+
247+
### **TypeScript**
248+
249+
```ts
250+
/**
251+
* Definition for a binary tree node.
252+
* class TreeNode {
253+
* val: number
254+
* left: TreeNode | null
255+
* right: TreeNode | null
256+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
257+
* this.val = (val===undefined ? 0 : val)
258+
* this.left = (left===undefined ? null : left)
259+
* this.right = (right===undefined ? null : right)
260+
* }
261+
* }
262+
*/
76263

264+
function flipMatchVoyage(root: TreeNode | null, voyage: number[]): number[] {
265+
let ok = true;
266+
let i = 0;
267+
const ans: number[] = [];
268+
const dfs = (root: TreeNode | null): void => {
269+
if (!root || !ok) {
270+
return;
271+
}
272+
if (root.val !== voyage[i++]) {
273+
ok = false;
274+
return;
275+
}
276+
if (!root.left || root.left.val === voyage[i]) {
277+
dfs(root.left);
278+
dfs(root.right);
279+
} else {
280+
ans.push(root.val);
281+
dfs(root.right);
282+
dfs(root.left);
283+
}
284+
};
285+
dfs(root);
286+
return ok ? ans : [-1];
287+
}
77288
```
78289

79290
### **...**

0 commit comments

Comments
 (0)