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

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

一. 二叉树相关算法题

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

思路
  • 本题可以利用二叉搜索树的有序性,自顶向下查找在[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){
            TreeNode left = lowestCommonAncestor(root.left, p, q);
            if (left != null) {
                return left;
            }
        }
        //区间右
        if (root.val<p.val && root.val <q.val){
            TreeNode right = lowestCommonAncestor(root.right, p, q);
            if (right != null) {
                return right;
            }
        }
        //区间中 , 因为根据题目一定存在根节点所以直接返回即可
        return root;
    }
}

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

递归法

思路
  • 以根节点为基础递归构建左右子树,如果节点为空那么说明找val的位置了
class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if (root == null) { //如果当前节点为空那么说明val找到了合适的位置
            TreeNode node = new TreeNode(val);
            return node;
        }

        if (root.val>val) root.left = insertIntoBST(root.left,val) ;  //递归创建左子树
        if (root.val<val) root.right = insertIntoBST(root.right,val); //递归创建右子树
        return root;
    }

}

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

递归(中序遍历)

思路
  • 根据二叉搜索树的特性判断向左或者向右搜索目标节点
  • 如果搜索到返回下一节点,最后递归返回根节点
  • 一共有五种情况
  1. 没有搜索到节点直接返回空
  2. 搜索到节点左右子树都为空直接删除返回null
  3. 搜索的节点只有一个左子树 返回左子树
  4. 搜索的节点只有一个右子树 返回右子树
  5. 左右子树都不为空:将右子树根节点覆盖当前节点左子树指向右子树左下角节点
class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
        // 递归终止条件
        //第一种情况没有找到要删除节点
        if(root == null) return null;
        if(root.val == key) {
             // 第一种情况 左右子树都为空
            if (root.left == null && root.right == null){
                root = null;
                return root;
            }
            // 第二种情况 左子树为空右子树不为空
            else if (root.left == null && root.right != null){
                return root.right;
            }
            // 第三种情况 左子树不为空右子树为空
            else if (root.left != null && root.right == null) {
                return root.left;
            }
            // 第四种情况 左右子树都不为空 :将左子树指向右子树最右边节点返回右子树根节点
            else {
                TreeNode node = root.right;
                // 查找右子树左下角
                while (node.left != null){
                    node = node.left;
                }
                node.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、付费专栏及课程。

余额充值