zoukankan      html  css  js  c++  java
  • [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 to correctBinaryTree)
    • int toNode (not available to correctBinaryTree)

    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 and toNode will exist in the tree and will be on the same depth.
    • toNode is to the right of fromNode.
    • fromNode.right is null 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 }

    LeetCode 题目总结

  • 相关阅读:
    Eclipse安装python注意事项
    C# 计算文件MD5
    C# 为私有方法添加单元测试(反射)
    .net 操作sftp服务器
    在ASP.NET MVC中使用Unity进行依赖注入的三种方式
    ASP.NET Web API 安全筛选器
    Token Based Authentication in Web API 2
    IIS中查看W3P.exe进程对应的应用程序池的方法
    WCF自定义Header
    sqlserver 用 RowNumber 分组
  • 原文地址:https://www.cnblogs.com/cnoodle/p/14088662.html
Copyright © 2011-2022 走看看