[豪の算法奇妙冒险] 代码随想录算法训练营第二十天 | 235-二叉搜索树的最近公共祖先、701-二叉搜索树中的插入操作、450-删除二叉搜索树中的节点

代码随想录算法训练营第二十天 | 235-二叉搜索树的最近公共祖先、701-二叉搜索树中的插入操作、450-删除二叉搜索树中的节点


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

题目链接:https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-search-tree/description/

文章讲解:https://programmercarl.com/0235.二叉搜索树的最近公共祖先.html

视频讲解:https://www.bilibili.com/video/BV1Zt4y1F7ww/?share_source=copy_web&vd_source=b989f2b109eb3b17e8178154a7de7a51

​ 采用 LeetCode236 二叉树的最近公共祖先 的思路AC了,但没利用上二叉搜索树有序的特性,继续优化

image-20251215155417329

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null || root == p || root == q){
            return root;
        }

        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);

        if(left == null && right == null){
            return null;
        }else if(left != null && right == null){
            return left;
        }else if(left == null && right != null){
            return right;
        }else{
            return root;
        }
    }
}

​ 如果 cur->val 大于 p->val,同时 cur->val 大于q->val,那么就应该向左遍历(说明目标区间在左子树上)

​ 如果 cur->val 小于 p->val,同时 cur->val 小于 q->val,那么就应该向右遍历(目标区间在右子树)

​ 剩下的情况,就是cur节点在区间(p->val <= cur->val && cur->val <= q->val)或者 (q->val <= cur->val && cur->val <= p->val)中,那么cur就是最近公共祖先了,直接返回cur

image-20251215161111681

class Solution{
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q){
        if(root == null){
            return null;
        }

        if(root.val > p.val && root.val > q.val){
            TreeNode left = lowestCommonAncestor(root.left, p, q);
            if(left != null){
                return left;
            }
        }else if(root.val < p.val && root.val < q.val){
            TreeNode right = lowestCommonAncestor(root.right, p, q);
            if(right != null){
                return right;
            }
        }

        return root;
    }
}

LeetCode701 二叉搜索树中的插入操作

题目链接:https://leetcode.cn/problems/insert-into-a-binary-search-tree/description/

文章讲解:https://programmercarl.com/0701.二叉搜索树中的插入操作.html

视频讲解:https://www.bilibili.com/video/BV1Et4y1c78Y/?share_source=copy_web&vd_source=b989f2b109eb3b17e8178154a7de7a51

​ 只要按照二叉搜索树的规则去遍历,遇到空节点就插入节点即可

image-20251215163643303

class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if(root == null){
            TreeNode newNode = new TreeNode(val);
            return newNode;
        }

        if(val < root.val){
           root.left = insertIntoBST(root.left, val);
        }
        if(val > root.val){
            root.right = insertIntoBST(root.right, val);
        }

        return root;
    }
}

LeetCode450 删除二叉搜索树中的节点

题目链接:https://leetcode.cn/problems/delete-node-in-a-bst/description/

文章讲解:https://programmercarl.com/0450.删除二叉搜索树中的节点.html

视频讲解:https://www.bilibili.com/video/BV1tP41177us/?share_source=copy_web&vd_source=b989f2b109eb3b17e8178154a7de7a51

​ 这题相较于二叉搜索树插入节点要难的多,它涉及到重构二叉树

​ 二叉搜索树中删除节点会遇到五种情况:

  • 第一种情况:没找到删除的节点,遍历到空节点,直接返回null
  • 找到删除的节点
    • 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点
    • 第三种情况:删除节点的左孩子为空,右孩子不为空,删除节点,右孩子补位,返回右孩子为根节点
    • 第四种情况:删除节点的左孩子不为空,右孩子为空,删除节点,左孩子补位,返回左孩子为根节点
    • 第五种情况:左右孩子节点都不为空,则将删除节点的左子树头结点(左孩子)放到删除节点的右子树的最左面节点的左孩子上,返回删除节点的右孩子为新的根节点

image-20251215175249944

class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
        if(root == null){
            return null;
        }else if(root.val == key){
            if(root.left == null && root.right == null){
                return null;
            }else if(root.left != null && root.right == null){
                return root.left;
            }else if(root.left == null && root.right != null){
                return root.right;
            }else{
                TreeNode cur = root.right;
                while(cur.left != null){
                    cur = cur.left;
                }
                cur.left = root.left;
                return root.right;
            }
        }

        if(key > root.val){
            root.right = deleteNode(root.right, key);
        }
        if(key < root.val){
            root.left = deleteNode(root.left, key);
        }
        return root;
    }
}
posted @ 2025-12-15 18:01  SchwarzShu  阅读(10)  评论(0)    收藏  举报