Day24:Leetcode:235. 二叉搜索树的最近公共祖先 + 701.二叉搜索树中的插入操作 + 450.删除二叉搜索树中的节点

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

解决方案:

1.思路

  • 对于当前节点x,如果x比p和q的值都大,说明,p和q在x的右子树里面,那么去x的右子树里面去寻找;
  • 对于当前节点x,如果x比p和q的值都小,说明,p和q在x的左子树里面,那么去x的左子树里面去寻找;

2.代码实现

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        TreeNode ancestor = root;
        while (true) {
            if (p.val < ancestor.val && q.val < ancestor.val) {
                ancestor = ancestor.left;
            } else if (p.val > ancestor.val && q.val > ancestor.val) {
                ancestor = ancestor.right;
            } else {
                break;
            }
        }
        return ancestor;
    }
}
  • 递归遍历
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        //假设p>q
        if(root.val>p.val && root.val>q.val){
           return lowestCommonAncestor(root.left, p, q);
        }
        if(root.val<q.val && root.val<p.val){
            return lowestCommonAncestor(root.right,  p,  q);
    }
    else{return root;}
  }
}

3.复杂度分析

  • 非递归
  • 时间复杂度为 O ( n ) O(n) O(n);
  • 空间复杂度为 O ( 1 ) O(1) O(1);
  • 递归
  • 时间复杂度为 O ( n ) O(n) O(n);
  • 空间复杂度为 O ( n ) O(n) O(n);

5.疑问

  • 问:该题有必要使用递归吗?
  • 答:没有必要,浪费空间复杂度;

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

解决方案:

1.思路:

  • 非递归思路

2.代码实现

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

3.复杂度分析

  • 时间复杂度为 O ( n ) O(n) O(n);
  • 空间复杂度为 O ( 1 ) O(1) O(1);

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

问题描述

解决方案:

1.思路:

2.代码实现

class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
        if (root == null) {
            return null;
        }
        if (root.val > key) {
            root.left = deleteNode(root.left, key);
            return root;
        }
        if (root.val < key) {
            root.right = deleteNode(root.right, key);
            return root;
        }
       //找到待删除节点:当root.val == key时,处理四种情况:
        if (root.val == key) {//节点无左右子树:直接删除该节点,返回null。
            if (root.left == null && root.right == null) {
                return null;
            }
            if (root.right == null) {//只有左子树:删除该节点,返回左子树作为新的根。
                return root.left;
            }
            if (root.left == null) {//只有右子树:删除该节点,返回右子树作为新的根。
                return root.right;
            }
//既有左子树又有右子树:找到右子树的最小节点(即最左边的节点,称为“后继节点”或“ inorder successor”),用这个后继节点替换当前节点,并删除原后继节点。这确保了BST的性质仍然被维持。
            TreeNode successor = root.right;
            while (successor.left != null) {
                successor = successor.left;
            }
//寻找后继节点及替换:后继节点的左子树为空,因此可以直接将它移到当前节点位置,并递归地从后继节点的右子树中删除后继节点(避免重复值问题)。
            root.right = deleteNode(root.right, successor.val);
            successor.right = root.right;
            successor.left = root.left;
            return successor;
        }
//经过上述处理后,最终返回调整后的树的根节点。
        return root;
    }
}

3.复杂度分析

在这里插入图片描述

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值