搜索二叉树BST的增删改查:力扣刷题

参看 https://mp.weixin.qq.com/s?__biz=MzAxODQxMDM0Mw==&mid=2247488128&idx=2&sn=b8fb3fd2917f9ac86127054741cd5877&chksm=9bd7ec88aca0659ee0185b657663169169493e9df2063fa4d28b38a0b4d0dd698d0301937898&cur_album_id=1318896187793260544&scene=189#rd

1.搜索二叉树的合法性

验证是否是BST(搜索二叉树):根据定义左小右大,且左右子树都是BST。

98. 验证二叉搜索树

给定一个二叉树,判断其是否是一个有效的二叉搜索树。

假设一个二叉搜索树具有如下特征:

节点的左子树只包含小于当前节点的数。
节点的右子树只包含大于当前节点的数。
所有左子树和右子树自身必须也是二叉搜索树。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isValidBST(TreeNode root) {
        return isValid(root,null,null);
    }
    private boolean isValid(TreeNode root, TreeNode min, TreeNode max){
        if (root == null) return true;
        // 若 root.val 不符合 max 和 min 的限制,说明不是合法 BST
        if (min != null && root.val <= min.val) return false;
        if (max != null && root.val >= max.val) return false;
        // 限定左子树的最大值是 root.val,右子树的最小值是 root.val
        return isValid(root.left, min, root) && isValid(root.right, root, max);
    }
}

2.BST中的搜索

利用BST左小右大的性质

700. 二叉搜索树中的搜索

给定二叉搜索树(BST)的根节点和一个值。 你需要在BST中找到节点值等于给定值的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 NULL。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode searchBST(TreeNode root, int val) {
        if(root == null || root.val == val) {
            return root;
        }
        //左小右大
        if(val < root.val){
            return searchBST(root.left, val);
        }
        if(val > root.val){
            return searchBST(root.right, val);
        }
        return null;
    }
}

3.BST的插入

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

  • 如果 root 是空,则新建树节点作为根节点返回即可。
  • 否则比较 root.val 与目标值的大小关系:
    • 如果 root.val 大于目标值,说明目标值应当插入 root 的左子树中,问题变为了在 root.left 中插入目标值,递归调用当前函数;
    • 如果 root.val 小于目标值,说明目标值应当插入 root 的右子树中,问题变为了在 root.right 中插入目标值,递归调用当前函数。
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
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);
        }
        if(val > root.val){
            root.right = insertIntoBST(root.right, val);
        }
        return root;
    }
}

4.BST的删除

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

这道题比上面都麻烦

删除节点时要考虑3种情况

情况 1A恰好是末端节点,两个子节点都为空,那么它可以当场去世了。

if (root.left == null && root.right == null)
    return null;

情况 2A只有一个非空子节点,那么它要让这个孩子接替自己的位置。

if (root.left == null) return root.right;
if (root.right == null) return root.left;

情况 3A有两个子节点,麻烦了,为了不破坏 BST 的性质,A必须找到左子树中最大的那个节点,或者右子树中最小的那个节点来接替自己。

if (root.left != null && root.right != null) {
    // 找到右子树的最小节点
    TreeNode minNode = getMin(root.right);
    // 把 root 改成 minNode
    root.val = minNode.val;
    // 转而去删除 minNode
    root.right = deleteNode(root.right, minNode.val);
}
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
        if(root == null) return null;
        if(root.val == key){
            /*
            注:删除节点时候,我们要注意,删除这个节点后,该树还是一个BST
            1.key正好是端尾节点(左右节点为空),直接删除
                if (root.left == null && root.right == null) return null;
            2.key有一个左节点或一个右节点,让左节点或右节点代替这个key位置
                if (root.left == null) return root.right;
                if (root.right == null) return root.left;
            3.key有两个节点
                if (root.left != null && root.right != null) {
                    // 找到右子树的最小节点
                    TreeNode minNode = getMin(root.right);
                    // 把 root 改成 minNode
                    root.val = minNode.val;
                    // 转而去删除 minNode
                    root.right = deleteNode(root.right, minNode.val);
                }
            */
            //包含1,2
            if(root.left == null) return root.right;
            if(root.right == null) return root.left;
            //3
            TreeNode minNode = getMin(root.right);
            root.val = minNode.val;
            root.right = deleteNode(root.right, minNode.val);
        }else if(root.val > key){
            root.left = deleteNode(root.left, key);
        }else{
            root.right = deleteNode(root.right ,key);
        }
        return root;
    }
    private TreeNode getMin(TreeNode node){
        // BST 最左边的就是最小的
        while(node.left != null)
            node = node.left;
        return node;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值