C++
Java
Python
Python3
C#
JavaScript
monokai
ambiance
chaos
chrome
cloud9_day
cloud9_night
cloud9_night_low_color
clouds
clouds_midnight
cobalt
crimson_editor
dawn
dracula
dreamweaver
eclipse
github
github_dark
gob
gruvbox
gruvbox_dark_hard
gruvbox_light_hard
idle_fingers
iplastic
katzenmilch
kr_theme
kuroir
merbivore
merbivore_soft
mono_industrial
nord_dark
one_dark
pastel_on_dark
solarized_dark
solarized_light
sqlserver
terminal
textmate
tomorrow
tomorrow_night
tomorrow_night_blue
tomorrow_night_bright
tomorrow_night_eighties
twilight
vibrant_ink
xcode
上次编辑到这里,代码来自缓存 点击恢复默认模板
/**
* 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:
TreeNode* correctBinaryTree(TreeNode* root) {
}
};
运行代码
提交
java 解法, 执行用时: 4 ms, 内存消耗: 44.4 MB, 提交时间: 2023-10-17 11:16:51
/**
* 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 {
boolean find ;
Set<Integer> set;
// dfs
public TreeNode correctBinaryTree(TreeNode root) {
find = false;
set = new HashSet<>();
dfs(root);
return root;
}
public TreeNode dfs(TreeNode root) {
if (root == null || find) return root;
if (root.right != null && set.contains(root.right.val)){
find = true;
return null;
}
set.add(root.val);
root.right = dfs(root.right);
root.left = dfs(root.left);
return root;
}
// bfs, 先右后左
public TreeNode correctBinaryTree1(TreeNode root) {
if (root == null) return null;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()){
TreeNode node = queue.poll();
if (node.right != null){
if (node.right.right != null && queue.contains(node.right.right)){
node.right = null;
return root;
}
queue.offer(node.right);
}
if (node.left != null){
if (node.left.right != null && queue.contains(node.left.right)){
node.left = null;
return root;
}
queue.offer(node.left);
}
}
return root;
}
}
cpp 解法, 执行用时: 104 ms, 内存消耗: 70.2 MB, 提交时间: 2023-10-17 11:11:43
/**
* 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:
TreeNode* correctBinaryTree(TreeNode* root) {
queue<TreeNode *> Q;
Q.push(root);
while (Q.size()) {
int cur_len = Q.size();
unordered_set<TreeNode*> nxt_level;
for (int ee = 0; ee <cur_len; ee ++) {
TreeNode *p = Q.front(); Q.pop(); //例2中的结点3
//注意每一层进队的顺序,从右往左进queue
if (p->right) { //结点7
if (nxt_level.count(p->right->right)){
//p->right -> right = nullptr;
p->right = nullptr;
return root;
} else {
Q.push(p->right);
nxt_level.insert(p->right);
}
}
if (p->left) {
if (nxt_level.count(p->left->right)){
//p->left->right = nullptr;
p->left = nullptr;
return root;
} else {
Q.push(p->left);
nxt_level.insert(p->left);
}
}
}
}
return root; //为了编译通过
}
};
python3 解法, 执行用时: 152 ms, 内存消耗: 30.1 MB, 提交时间: 2023-10-17 11:09:43
# 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 correctBinaryTree(self, root: TreeNode) -> TreeNode:
# BFS,每一层从右往左,看结点是否重复出现。本层讨论的对象是下一层
Q = [root]
while Q:
cur_len = len(Q)
nxt_level = set()
for _ in range(cur_len):
p = Q.pop(0)
if p.right:
if p.right.right in nxt_level:
p.right = None
return root
else:
Q.append(p.right)
nxt_level.add(p.right)
if p.left:
if p.left.right in nxt_level:
p.left = None
return root
else:
Q.append(p.left)
nxt_level.add(p.left)