二叉搜索树

目录

 一、二叉搜索树的特点

 二、二叉搜索树中搜索

三、验证二叉搜索树

四、二叉搜索树的最小绝对差

五、二叉搜索树中的众数

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

七、二叉搜索树中的插入操作

八、删除二叉搜索树中的节点

九、将有序数组转换为二叉搜索树

十、修剪二叉搜索树

十一、把二叉搜索树转换为累加树


 一、二叉搜索树的特点

1、每个节点值都大于其左子树中所有节点的值,并且小于其右子树中所有节点的值。

2、二叉搜索树中没有值相等的节点。

3、二叉搜索树的左子树和右子树也都是二叉搜索树。

 二、二叉搜索树中搜索

700. 二叉搜索树中的搜索 - 力扣(LeetCode)

//递归法
/**
 * 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 searchBST(TreeNode root, int val) {
        if(root==null||root.val==val)return root;
        if(root.val>val)return searchBST(root.left,val);
        else return searchBST(root.right,val);
    }
}

三、验证二叉搜索树

98. 验证二叉搜索树 - 力扣(LeetCode)

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

四、二叉搜索树的最小绝对差

530. 二叉搜索树的最小绝对差 - 力扣(LeetCode)

783. 二叉搜索树节点最小距离 - 力扣(LeetCode)

class Solution {
    public TreeNode pre=null;
    public int result=Integer.MAX_VALUE;
    public int getMinimumDifference(TreeNode root) {
        traversal(root);
        return result;
    }
    public void traversal(TreeNode node){
        if(node==null)return;
        traversal(node.left);
        if(pre!=null)result=Math.min(result,Math.abs(node.val-pre.val));
        pre=node;
        traversal(node.right);
    }
}

五、二叉搜索树中的众数

501. 二叉搜索树中的众数 - 力扣(LeetCode)

class Solution {
    int max;
    int count;
    TreeNode pre;
    ArrayList result;
    public int[] findMode(TreeNode root) {
        result=new ArrayList<>();
        count=0;
        max=0;
        pre=null;
        result.clear();
        search(root);
        int[] res = new int[result.size()];
        for (int i = 0; i < result.size(); i++) {
            res[i] = result.get(i);
        }
        return res;
    }
    public void search(TreeNode root){
        if(root==null)return;
        search(root.left);

        if(pre==null||root.val!=pre.val)count=1;
        else count++;

        if(max==count)result.add(root.val);
        else if(max

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

235. 二叉搜索树的最近公共祖先 - 力扣(LeetCode)

//递归法
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root.val>p.val&&root.val>q.val)return lowestCommonAncestor(root.left,p,q);
        if(root.valp.val&&root.val>q.val)root=root.left;
            else if(root.val

七、二叉搜索树中的插入操作

701. 二叉搜索树中的插入操作 - 力扣(LeetCode)

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

八、删除二叉搜索树中的节点

450. 删除二叉搜索树中的节点 - 力扣(LeetCode)

情况1:没找到删除的节点,遍历空节点直接返回

情况2:找到删除节点,左右孩子都为null,直接删除节点,返回null为根节点

情况3:找到删除节点,左孩子为null,右孩子不为null,删除节点,右孩子补位,返回右孩子为根节点

情况4:找到删除节点,左孩子不为null,右孩子为null,删除节点,左孩子补位,返回左孩子为根节点

情况5:找到删除节点,左右孩子都不为null,将删除节点的左子树头结点放到删除节点的右子树的最左面节点的左孩子上,返回删除节点右孩子为新的根节点(看图理解)

二叉搜索树_第1张图片

class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
        if(root==null)return root;//情况1
        if(root.val==key){
            if(root.left==null&&root.right==null)return null;//情况2
            else if (root.left==null&&root.right!=null)return root.right;//情况3
            else if (root.right==null&&root.left!=null)return root.left;//情况4
            else{//情况5
                TreeNode cur=root.right;
                while(cur.left!=null)cur=cur.left;
                cur.left=root.left;
                root=root.right;
                return root;
            }
        }
        if(root.val>key)root.left=deleteNode(root.left,key);
        if(root.val

九、将有序数组转换为二叉搜索树

108. 将有序数组转换为二叉搜索树 - 力扣(LeetCode)

class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        return traversal(nums,0,nums.length);
    }
    public TreeNode traversal(int[] nums,int left,int right){
        if(left>=right)return null;
        if(right-left==1)return new TreeNode(nums[left]);
        int mid=left+(right-left)/2;
        TreeNode root=new TreeNode(nums[mid]);
        root.left=traversal(nums,left,mid);
        root.right=traversal(nums,mid+1,right);
        return root;
    }
}

十、修剪二叉搜索树

669. 修剪二叉搜索树 - 力扣(LeetCode)

class Solution {
    public TreeNode trimBST(TreeNode root, int low, int high) {
        if(root==null)return null;
        if(root.val>high)return trimBST(root.left,low,high);
        if(root.val

十一、把二叉搜索树转换为累加树

538. 把二叉搜索树转换为累加树 - 力扣(LeetCode)

二叉搜索树_第2张图片

class Solution {
    public int prevalue=0;
    public TreeNode convertBST(TreeNode root) {
        traversal(root);
        return root;
    }
    public void traversal(TreeNode root){
        if(root==null)return;
        traversal(root.right);
        root.val+=prevalue;
        prevalue=root.val;
        traversal(root.left);
    }
}

你可能感兴趣的:(LeetCode,算法,leetcode,职场和发展)