算法刷题|235.二叉搜索树的最近公共祖先、701.二叉搜索树中的插入操作、450.删除二叉搜索树中的节点

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

题目:给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。

百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”

递归

思路:因为是二叉搜索树,我们就可以利用二叉搜索树的特性,如果当前节点的值大于p和q的值,说明p和q在当前节点的左子树中,否则在右子树中

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){
            // 说明需要往左子树遍历
            return lowestCommonAncestor(root.left,p,q);
        }
        if(root.val < p.val && root.val < q.val){
            // 往右子树遍历
            return lowestCommonAncestor(root.right,p,q);
        }
        return root;
    }
}

迭代

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
       while(root != null){
           if(root.val > p.val && root.val > q.val){
               root = root.left;
               continue;
           }
           if(root.val < p.val && root.val < q.val){
               root = root.right;
               continue;
           }
           return root;
       }
       return null;
    }
}

二叉搜索树中的插入操作

题目:给定二叉搜索树(BST)的根节点 root 和要插入树中的值 value ,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据 保证 ,新值和原始二叉搜索树中的任意节点值都不同。

注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回 任意有效的结果 。

递归

思路:因为是二叉搜索树所以插入的元素一定可以在叶子节点处插入,不理解可以找个二叉树带入值试试,一定可以插入到叶子节点处

class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if(root == null){// 一定可以在叶子节点处插入,所以这里返回插入的节点
            return new TreeNode(val);
        }
        if(root.val > val){
            // 往左子树递归
            // 这里为什么要用root.right来接返回值呢,以为我的终止条件会返回插入节点,这里接住了之后才能连接起来
            root.left = insertIntoBST(root.left,val);
        }
        if(root.val < val){
            // 往右子树递归
            // 同上
            root.right = insertIntoBST(root.right,val);
        }
        return root;
    }
}

迭代

class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if(root == null){
            return new TreeNode(val);
        }
        TreeNode node = root;
        while(node != null){
            if(node.val > val){
                if(node.left == null){
                    node.left = new TreeNode(val);
                    break;
                }else{
                    node = node.left;
                }
            }
            if(node.val < val){
                if(node.right == null){
                    node.right = new TreeNode(val);
                    break;
                }else{
                    node = node.right;
                }
            }
        }
        return root;
    }
}

删除二叉搜索树中的节点

题目:给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。

一般来说,删除节点可分为两个步骤:

首先找到需要删除的节点;
如果找到了,删除它。

思路:被删除的节点总共的情况有:1.叶子节点;2.左子树不为空右子树为空;3.左子树为空右子树不为空;4左右子树都不为空;然后根据每种情况做删除操作

class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
        if(root == null){// 终止条件1
            return null;
        }
        // 终止条件2
        if(root.val == key){
            // 找到了要删除的元素
            if(root.left == null && root.right == null){
                // 删除的是叶子节点,直接向上返回null就行,然后后面父节点接住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(root.val > key){
            root.left = deleteNode(root.left,key);
        }
        // 往右递归
        if(root.val < key){
            root.right = deleteNode(root.right,key);
        }
        return root;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值