Skip to content

Commit 2cf8472

Browse files
committed
feat: add solutions to lc problem: No.1660
No.1660.Correct a Binary Tree
1 parent e2f0633 commit 2cf8472

File tree

6 files changed

+198
-255
lines changed

6 files changed

+198
-255
lines changed

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

+70-100
Original file line numberDiff line numberDiff line change
@@ -62,49 +62,19 @@
6262

6363
<!-- 这里可写通用的实现逻辑 -->
6464

65-
<!-- tabs:start -->
65+
**方法一:DFS**
6666

67-
### **Python3**
67+
我们设计一个函数 $dfs(root)$,用于处理以 $root$ 为根的子树。如果 $root$ 为 $null$ 或者 $root.right$ 已经被访问过,说明 $root$ 为无效节点,返回 $null$。否则,递归处理 $root.right$ 和 $root.left$,并返回 $root$。
6868

69-
<!-- 这里可写当前语言的特殊实现逻辑 -->
69+
最后,返回 $dfs(root)$ 即可。
7070

71-
记录父节点
71+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为二叉树节点个数
7272

73-
```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 = 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-
```
73+
<!-- tabs:start -->
74+
75+
### **Python3**
10676

107-
优化,无需记录父节点。
77+
<!-- 这里可写当前语言的特殊实现逻辑 -->
10878

10979
```python
11080
# Definition for a binary tree node.
@@ -115,22 +85,16 @@ class Solution:
11585
# self.right = right
11686
class Solution:
11787
def correctBinaryTree(self, root: TreeNode) -> TreeNode:
118-
q = 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
88+
def dfs(root):
89+
if root is None or root.right in vis:
90+
return None
91+
vis.add(root)
92+
root.right = dfs(root.right)
93+
root.left = dfs(root.left)
94+
return root
95+
96+
vis = set()
97+
return dfs(root)
13498
```
13599

136100
### **Java**
@@ -154,29 +118,19 @@ class Solution:
154118
* }
155119
*/
156120
class Solution {
121+
private Set<TreeNode> vis = new HashSet<>();
122+
157123
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-
}
124+
return dfs(root);
125+
}
126+
127+
private TreeNode dfs(TreeNode root) {
128+
if (root == null || vis.contains(root.right)) {
129+
return null;
179130
}
131+
vis.add(root);
132+
root.right = dfs(root.right);
133+
root.left = dfs(root.left);
180134
return root;
181135
}
182136
}
@@ -199,34 +153,50 @@ class Solution {
199153
class Solution {
200154
public:
201155
TreeNode* correctBinaryTree(TreeNode* root) {
202-
queue<TreeNode*> q;
203-
q.push(root);
204-
unordered_set<TreeNode*> s;
205-
while (!q.empty()) {
206-
int n = q.size();
207-
while (n--) {
208-
TreeNode* node = q.front();
209-
q.pop();
210-
if (node->right) {
211-
if (s.count(node->right->right)) {
212-
node->right = nullptr;
213-
return root;
214-
}
215-
q.push(node->right);
216-
s.insert(node->right);
217-
}
218-
if (node->left) {
219-
if (s.count(node->left->right)) {
220-
node->left = nullptr;
221-
return root;
222-
}
223-
q.push(node->left);
224-
s.insert(node->left);
225-
}
156+
unordered_set<TreeNode*> vis;
157+
function<TreeNode*(TreeNode*)> dfs = [&](TreeNode* root) -> TreeNode* {
158+
if (!root || vis.count(root->right)) {
159+
return nullptr;
226160
}
161+
vis.insert(root);
162+
root->right = dfs(root->right);
163+
root->left = dfs(root->left);
164+
return root;
165+
};
166+
return dfs(root);
167+
}
168+
};
169+
```
170+
171+
### **JavaScript**
172+
173+
```js
174+
/**
175+
* Definition for a binary tree node.
176+
* function TreeNode(val, left, right) {
177+
* this.val = (val===undefined ? 0 : val)
178+
* this.left = (left===undefined ? null : left)
179+
* this.right = (right===undefined ? null : right)
180+
* }
181+
*/
182+
/**
183+
* @param {TreeNode} root
184+
* @param {number} from
185+
* @param {number} to
186+
* @return {TreeNode}
187+
*/
188+
var correctBinaryTree = function (root) {
189+
const dfs = root => {
190+
if (!root || vis.has(root.right)) {
191+
return null;
227192
}
193+
vis.add(root);
194+
root.right = dfs(root.right);
195+
root.left = dfs(root.left);
228196
return root;
229-
}
197+
};
198+
const vis = new Set();
199+
return dfs(root);
230200
};
231201
```
232202

0 commit comments

Comments
 (0)