力扣算法 Java 刷题笔记【二叉搜索树 BST 篇】hot100(二)BST 的基础操作:判断 BST 的合法性、增删改查 4

1. 验证二叉搜索树(中等)

地址: https://leetcode-cn.com/problems/validate-binary-search-tree/
2021/12/03
做题反思:我们通过使用辅助函数,增加函数参数列表,在参数中携带额外信息,将这种约束传递给子树的所有节点,这也是二叉树算法的一个小技巧吧。

class Solution {
    
    
    public boolean isValidBST(TreeNode root) {
    
    
        return isValidBST(root, null, null);
    }
    boolean isValidBST(TreeNode root, TreeNode min, TreeNode max) {
    
    
        if (root == null) {
    
    
            return true;
        }
        if (min != null && root.val <= min.val) {
    
    
            return false;
        }
        if (max != null && root.val >= max.val) {
    
    
            return false;
        }
        return isValidBST(root.left, min, root) &&
            isValidBST(root.right, root, max);
    }
}

2. 二叉搜索树中的搜索 (简单)

地址: https://leetcode-cn.com/problems/search-in-a-binary-search-tree/
2021/12/02
做题反思:root .val 和 val 比较

class Solution {
    
    
    public TreeNode searchBST(TreeNode root, int val) {
    
    
        if (root == null) {
    
    
            return null;
        }
        if (root.val < val) {
    
    
            return searchBST(root.right, val);
        }
        if (root.val > val) {
    
    
            return searchBST(root.left, val);
        }
        return root;
    }
}

3. 二叉搜索树中的插入操作 (中等)

地址: https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/
2021/12/02
做题反思:基线条件; 对左右子树递归赋值

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

在这里插入图片描述

4. 删除二叉搜索树中的节点 (中等)

地址: https://leetcode-cn.com/problems/delete-node-in-a-bst/
2021/12/03
做题反思:

class Solution {
    
    
    public TreeNode deleteNode(TreeNode root, int key) {
    
    
        if (root == null) {
    
    
            return null;
        }
        if (root.val == key) {
    
    
            if (root.left == null) {
    
    
                return root.right;
            }
            if (root.right == null) {
    
    
                return root.left;
            }
            TreeNode minNode = getMin(root.right);
            root.val = minNode.val;
            root.right = deleteNode(root.right, minNode.val);
        } else if (root.val < key) {
    
    
            root.right = deleteNode(root.right, key);
        } else if (root.val > key) {
    
    
            root.left = deleteNode(root.left, key);
        }
        return root;
    }

    TreeNode getMin(TreeNode root) {
    
    
        while (root.left != null) {
    
    
            root = root.left;
        }
        return root;
    }
}

在这里插入图片描述
同理,由 BST 的性质,删除时也可从左子树入手
解法二:

class Solution {
    
    
    public TreeNode deleteNode(TreeNode root, int key) {
    
    
        if (root == null) {
    
    
            return null;
        }
        if (root.val == key) {
    
    
            if (root.left == null) {
    
    
                return root.right;
            }
            if (root.right == null) {
    
    
                return root.left;
            }
            TreeNode maxNode = getMax(root.left);
            root.val = maxNode.val;
            root.left = deleteNode(root.left, maxNode.val);
        } else if (root.val < key) {
    
    
            root.right = deleteNode(root.right, key);
        } else if (root.val > key) {
    
    
            root.left = deleteNode(root.left, key);
        }
        return root;
    }

    TreeNode getMax(TreeNode root) {
    
    
        while (root.right != null) {
    
    
            root = root.right;
        }
        return root;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_46644403/article/details/121589189
今日推荐