LeeCode Practice Journal | Day20_Binary Tree07

235.二叉搜索树的最近公共祖先

题目:235. 二叉搜索树的最近公共祖先 - 力扣(LeetCode)
题解:代码随想录 (programmercarl.com)
递归的查找左右子树的条件判断更加简单了

solution:
public class Solution {
    public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(p.val < root.val && q.val < root.val)
            return LowestCommonAncestor(root.left, p, q);
        else if(p.val > root.val && q.val > root.val)
            return LowestCommonAncestor(root.right, p, q);
        else
            return root;
    }
}
summary:

701.二叉搜索树中的插入操作

题目:701. 二叉搜索树中的插入操作 - 力扣(LeetCode)
题解:代码随想录 (programmercarl.com)
递归的像子树中插入节点,并返回插入节点的子树

solution:
public class Solution {
    public TreeNode InsertIntoBST(TreeNode root, int val) {
        if(root == null) return new TreeNode(val);

        if(val < root.val) root.left = InsertIntoBST(root.left, val);
        else root.right = InsertIntoBST(root.right, val);
        return root;
    }
}
summary:

错误:

插入节点操作只创建了新节点,没有让父节点指向新节点,忽略了回溯

450.删除二叉搜索树中的节点

题目:450. 删除二叉搜索树中的节点 - 力扣(LeetCode)
题解:代码随想录 (programmercarl.com)
怎么样简洁的进行删除操作纠结了好一会

solution:
public class Solution {
    public TreeNode cur = null;
    public TreeNode DeleteNode(TreeNode root, int key) {
        if(root == null) return null;

        if(key < root.val) root.left = DeleteNode(root.left, key);
        else if(key > root.val) root.right = DeleteNode(root.right, key);
        else
        {
            if(root.right != null)
            {
                cur = root.right;
                while(cur.left != null) cur = cur.left;
                cur.left = root.left;
                return root.right;
            }
            else return root.left;
        }

        return root;
    }
}
summary:

key:

二叉搜索树节点的删除操作:
存在右子树时:
        删除节点的左子树成为右子树的最左节点的左子树
        删除节点的父节点指向右子树
        (顺序不可颠倒)
不存在右子树时:
        返回左子树

错误:

以为需要记录一个pre节点

删除操作的细节:
需要判断右子树是否存在

最后的return:
整个算法的逻辑用自然语言描述是若目标在节点的左子树则对左子树递归,在右子树则对右子树递归,最后返回节点,所以对左右子树操作完成后,要返回当前节点
return的本质:删除操作后的二叉树

相关推荐

  1. 周末总结(2024/07/20)

    2024-07-22 12:42:02       19 阅读
  2. ubuntu20.04.3

    2024-07-22 12:42:02       60 阅读
  3. Ubuntu20.04

    2024-07-22 12:42:02       38 阅读
  4. Ubuntu20.04配置

    2024-07-22 12:42:02       36 阅读
  5. 大模型日报 2024-07-20

    2024-07-22 12:42:02       14 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-22 12:42:02       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-22 12:42:02       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-22 12:42:02       45 阅读
  4. Python语言-面向对象

    2024-07-22 12:42:02       55 阅读

热门阅读

  1. CSS(层叠样式表)选择器

    2024-07-22 12:42:02       18 阅读
  2. 查询优化 -- UNION 用法

    2024-07-22 12:42:02       16 阅读
  3. C# struct里面的class是值类型还是引用类型

    2024-07-22 12:42:02       21 阅读
  4. 网络安全-网络安全及其防护措施11

    2024-07-22 12:42:02       20 阅读
  5. 算法训练营 day14 | 二叉树 part02

    2024-07-22 12:42:02       22 阅读
  6. Python爬虫技术 第08节 Cookies和Session

    2024-07-22 12:42:02       18 阅读
  7. JDK、JRE、JVM之间的关系

    2024-07-22 12:42:02       17 阅读