[LeetCode] 1660. Correct a Binary Tree
You have a binary tree with a small defect. There is exactly one invalid node where its right child incorrectly points to another node at the same depth but to the invalid node's right.
Given the root of the binary tree with this defect, root
, return the root of the binary tree after removing this invalid node and every node underneath it (minus the node it incorrectly points to).
Custom testing:
The test input is read as 3 lines:
TreeNode root
int fromNode
(not available tocorrectBinaryTree
)int toNode
(not available tocorrectBinaryTree
)
After the binary tree rooted at root
is parsed, the TreeNode
with value of fromNode
will have its right child pointer pointing to the TreeNode
with a value of toNode
. Then, root
is passed to correctBinaryTree
.
Example 1:
Input: root = [1,2,3], fromNode = 2, toNode = 3 Output: [1,null,3] Explanation: The node with value 2 is invalid, so remove it.
Example 2:
Input: root = [8,3,1,7,null,9,4,2,null,null,null,5,6], fromNode = 7, toNode = 4 Output: [8,3,1,null,null,9,4,null,null,5,6] Explanation: The node with value 7 is invalid, so remove it and the node underneath it, node 2.
Constraints:
- The number of nodes in the tree is in the range
[3, 104]
. -109 <= Node.val <= 109
- All
Node.val
are unique. fromNode != toNode
fromNode
andtoNode
will exist in the tree and will be on the same depth.toNode
is to the right offromNode
.fromNode.right
isnull
in the initial tree from the test data.
纠正二叉树。
你有一棵二叉树,这棵二叉树有个小问题,其中有且只有一个无效节点,它的右子节点错误地指向了与其在同一层且在其右侧的一个其他节点。
给定一棵这样的问题二叉树的根节点 root ,将该无效节点及其所有子节点移除(除被错误指向的节点外),然后返回新二叉树的根结点。
自定义测试用例:
测试用例的输入由三行组成:
TreeNode root
int fromNode (在 correctBinaryTree 中不可见)
int toNode (在 correctBinaryTree 中不可见)
当以 root 为根的二叉树被解析后,值为 fromNode 的节点 TreeNode 将其右子节点指向值为 toNode 的节点 TreeNode 。然后, root 传入 correctBinaryTree 的参数中。来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/correct-a-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路是BFS层序遍历。因为有错误的是某个node的右指针,所以我们的层序遍历会有一点变化,对于每一层的节点,我们从右往左遍历,遍历的时候我们把当前节点的左右孩子放入hashset,在遍历的时候,同时试探hashset里面是否已经存在左/右孩子的右孩子了,如果存在,说明左/右孩子的右指针指向了当前层的某个节点。我们去掉这个指向有错误的孩子即可。
时间O(n)
空间O(n)
Java实现
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode() {} 8 * TreeNode(int val) { this.val = val; } 9 * TreeNode(int val, TreeNode left, TreeNode right) { 10 * this.val = val; 11 * this.left = left; 12 * this.right = right; 13 * } 14 * } 15 */ 16 class Solution { 17 public TreeNode correctBinaryTree(TreeNode root) { 18 Queue<TreeNode> queue = new LinkedList(); 19 HashSet<TreeNode> set = new HashSet<>(); 20 queue.offer(root); 21 int size = 1; 22 while (size > 0) { 23 TreeNode cur = queue.poll(); 24 size--; 25 if (cur.right != null) { 26 set.add(cur.right); 27 queue.offer(cur.right); 28 // cur.right.right本应该再下一层,如果出现在当前层则说明这个节点的right指针连错了 29 if (set.contains(cur.right.right)) { 30 cur.right = null; 31 break; 32 } 33 } 34 if (cur.left != null) { 35 set.add(cur.left); 36 queue.offer(cur.left); 37 // cur.left.right本应该再下一层,如果出现在当前层则说明这个节点的right指针连错了 38 if (set.contains(cur.left.right)) { 39 cur.left = null; 40 break; 41 } 42 } 43 if (size == 0) { 44 size = queue.size(); 45 set = new HashSet<>(); 46 } 47 } 48 return root; 49 } 50 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具