Skip to content

Commit 838e4e3

Browse files
committed
feat: add solutions to lc problem: No.1660.Correct a Binary Tree
1 parent 717c6db commit 838e4e3

File tree

8 files changed

+408
-35
lines changed

8 files changed

+408
-35
lines changed

.github/workflows/starcharts.yml

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ jobs:
1414
with:
1515
github_token: ${{ secrets.GITHUB_TOKEN }}
1616
svg_path: images/starcharts.svg
17+
commit_message: "chore: update starcharts to leetcode project @doocs"

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@
254254

255255
<a href="https://gitpod.io/#https://github.com/doocs/leetcode" target="_blank" alt="Open in Gitpod"><img src="https://gitpod.io/button/open-in-gitpod.svg"></a>
256256

257+
## Stars 趋势
258+
259+
<a href="https://github.com/doocs/leetcode/stargazers" target="_blank"><img src="./images/starcharts.svg" alt="Stargazers over time" /></a>
260+
257261
## 贡献者
258262

259263
非常感谢以下所有朋友对本项目的贡献,你们是最可爱的人!
@@ -276,10 +280,6 @@
276280

277281
<a href="https://weibo.com/fly51fly" target="_blank"><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/images/recommender-fly51fly.png"></a>
278282

279-
## Star 趋势
280-
281-
![Stargazers over time](./images/starcharts.svg)
282-
283283
## 许可证
284284

285285
<a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">知识共享 版权归属-相同方式共享 4.0 国际 公共许可证</a>

README_EN.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ You can also contribute to [doocs/leetcode](https://github.com/doocs/leetcode) u
244244

245245
<a href="https://gitpod.io/#https://github.com/doocs/leetcode" target="_blank" alt="Open in Gitpod"><img src="https://gitpod.io/button/open-in-gitpod.svg"></a>
246246

247+
## Stargazers over time
248+
249+
<a href="https://github.com/doocs/leetcode/stargazers" target="_blank"><img src="./images/starcharts.svg" alt="Stargazers over time" /></a>
250+
247251
## Contributors
248252

249253
This project exists thanks to all the people who contribute.
@@ -260,10 +264,6 @@ Thank you to all our backers and sponsors!
260264

261265
> "_You help the developer community practice for interviews, and there is nothing better we could ask for._" -- [Alan Yessenbayev](https://opencollective.com/alan-yessenbayev)
262266
263-
## Stargazers over time
264-
265-
![Stargazers over time](./images/starcharts.svg)
266-
267267
## License
268268

269269
This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International License</a>.

solution/1600-1699/1660.Correct a Binary Tree/README.md

+156-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
<li><code>fromNode.right</code> 在测试用例的树中建立后为 <code>null</code> 。</li>
5959
</ul>
6060

61-
6261
## 解法
6362

6463
<!-- 这里可写通用的实现逻辑 -->
@@ -69,16 +68,172 @@
6968

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

71+
记录父节点。
72+
7273
```python
74+
# Definition for a binary tree node.
75+
# class TreeNode:
76+
# def __init__(self, val=0, left=None, right=None):
77+
# self.val = val
78+
# self.left = left
79+
# self.right = right
80+
class Solution:
81+
def correctBinaryTree(self, root: TreeNode) -> TreeNode:
82+
q = collections.deque([root])
83+
res = root
84+
p = {}
85+
while q:
86+
n = len(q)
87+
mp = {}
88+
for _ in range(n):
89+
node = q.popleft()
90+
if node.val in mp:
91+
left, father = p[mp[node.val]]
92+
if left:
93+
father.left = None
94+
else:
95+
father.right = None
96+
return res
97+
if node.left:
98+
q.append(node.left)
99+
p[node.left.val] = [True, node]
100+
if node.right:
101+
q.append(node.right)
102+
p[node.right.val] = [False, node]
103+
mp[node.right.val] = node.val
104+
return res
105+
```
73106

107+
优化,无需记录父节点。
108+
109+
```python
110+
# Definition for a binary tree node.
111+
# class TreeNode:
112+
# def __init__(self, val=0, left=None, right=None):
113+
# self.val = val
114+
# self.left = left
115+
# self.right = right
116+
class Solution:
117+
def correctBinaryTree(self, root: TreeNode) -> TreeNode:
118+
q = collections.deque([root])
119+
while q:
120+
n = len(q)
121+
for _ in range(n):
122+
node = q.popleft()
123+
if node.right:
124+
if node.right.right in q:
125+
node.right = None
126+
return root
127+
q.append(node.right)
128+
if node.left:
129+
if node.left.right in q:
130+
node.left = None
131+
return root
132+
q.append(node.left)
133+
return root
74134
```
75135

76136
### **Java**
77137

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

80140
```java
141+
/**
142+
* Definition for a binary tree node.
143+
* public class TreeNode {
144+
* int val;
145+
* TreeNode left;
146+
* TreeNode right;
147+
* TreeNode() {}
148+
* TreeNode(int val) { this.val = val; }
149+
* TreeNode(int val, TreeNode left, TreeNode right) {
150+
* this.val = val;
151+
* this.left = left;
152+
* this.right = right;
153+
* }
154+
* }
155+
*/
156+
class Solution {
157+
public TreeNode correctBinaryTree(TreeNode root) {
158+
Deque<TreeNode> q = new ArrayDeque<>();
159+
q.offer(root);
160+
while (!q.isEmpty()) {
161+
int n = q.size();
162+
while (n-- > 0) {
163+
TreeNode node = q.pollFirst();
164+
if (node.right != null) {
165+
if (node.right.right != null && q.contains(node.right.right)) {
166+
node.right = null;
167+
return root;
168+
}
169+
q.offer(node.right);
170+
}
171+
if (node.left != null) {
172+
if (node.left.right != null && q.contains(node.left.right)) {
173+
node.left = null;
174+
return root;
175+
}
176+
q.offer(node.left);
177+
}
178+
}
179+
}
180+
return root;
181+
}
182+
}
183+
```
81184

185+
### **C++**
186+
187+
```cpp
188+
/**
189+
* Definition for a binary tree node.
190+
* struct TreeNode {
191+
* int val;
192+
* TreeNode *left;
193+
* TreeNode *right;
194+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
195+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
196+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
197+
* };
198+
*/
199+
class Solution {
200+
public:
201+
TreeNode* correctBinaryTree(TreeNode* root) {
202+
queue<TreeNode*> q;
203+
q.push(root);
204+
unordered_set<TreeNode*> s;
205+
while (!q.empty())
206+
{
207+
int n = q.size();
208+
while (n--)
209+
{
210+
TreeNode* node = q.front();
211+
q.pop();
212+
if (node->right)
213+
{
214+
if (s.count(node->right->right))
215+
{
216+
node->right = nullptr;
217+
return root;
218+
}
219+
q.push(node->right);
220+
s.insert(node->right);
221+
}
222+
if (node->left)
223+
{
224+
if (s.count(node->left->right))
225+
{
226+
node->left = nullptr;
227+
return root;
228+
}
229+
q.push(node->left);
230+
s.insert(node->left);
231+
}
232+
}
233+
}
234+
return root;
235+
}
236+
};
82237
```
83238
84239
### **...**

0 commit comments

Comments
 (0)