【二叉搜索树】BST相关题目

二叉搜索树中的众树

501.二叉搜索树中的众树
在这里插入图片描述

解题思路:中序遍历二叉搜索树,使得结果集是有序的,过程中将众数个数保存下来。利用两个变量,一个记录当前相同众数数的个数,一个记录众数数的个数。
虽然题目没要求返回众数集的元素顺序,但这最后返回的是按升序来的,因为中序遍历得出的是有序的。

/**
 * 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 {
    private int curCnt = 1;// 记录当前众数最大个数
    private int maxCnt = 1;// 记录总的最大众数最大个数
    private TreeNode preNode = null;// 记录前一个节点(中序遍历前一个节点,并非父节点)
    public int[] findMode(TreeNode root) {
        List<Integer> numList = new ArrayList<>();
        inOrder(numList,root);
        int[] ans = new int[numList.size()];
        int index = 0;
        for(Integer num:numList)
            ans[index++] = num;
        return ans;
    }
    private void inOrder(List<Integer> numList,TreeNode node){
        if(node==null) return;
        inOrder(numList,node.left);
        if(preNode!=null){
            if(preNode.val==node.val)
                curCnt++;
            else
                curCnt = 1;
        }
        if(curCnt>maxCnt){
            maxCnt = curCnt;
            numList.clear();
            numList.add(node.val);
        }else if(curCnt==maxCnt){
            numList.add(node.val);
        }
        preNode = node;
        inOrder(numList,node.right);
    }
}

测试:

在这里插入图片描述

二叉搜索树节点最小距离

783.二叉搜索树节点最小距离

在这里插入图片描述

解题思路:两节点最短距离,肯定是相邻的两个节点,中序遍历升序的情况下即可遍历出最短距离了。和上面一题一样,设置一个 preNode 变量表示中序列的前一个节点。

/**
 * 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 {
    private int cutDis = (int)1e5;// 记录当前的两节点的距离
    private int minDis = (int)1e5;// 记录最小俩节点的距离
    private TreeNode preNode = null;// 表示前一个节点
    public int minDiffInBST(TreeNode root) {
        inOrder(root);
        return minDis;
    }
    private void inOrder(TreeNode node){
        if(node==null) return;
        inOrder(node.left);
        if(preNode!=null)
            cutDis = node.val - preNode.val;
        if(minDis>cutDis)
            minDis = cutDis;
        preNode = node;
        inOrder(node.right);
    }
}

测试:

在这里插入图片描述

两数之和 IV - 输入二叉搜索树

653. 两数之和 IV - 输入二叉搜索树

在这里插入图片描述

解题思路:中序遍历 + 双指针;这里的双指针用法是:左指针指向的数与右指针指向的数的和与实际值比较,如何比实际值大右指针就向左移一位,反之左指针向右移一个。

/**
 * 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 {
    private List<Integer> list = new ArrayList<>();
    public boolean findTarget(TreeNode root, int k) {
        inOrder(root);
        Integer[] test = new Integer[0];
        Integer[] nums = list.toArray(test);// 个人喜欢数组,这里可以不转换
        int l=0, r=nums.length-1;
        while(l<r){
            if(nums[l]+nums[r]<k)
                ++l;
            else if(nums[l]+nums[r]>k)
                --r;
            else
                return true;
        }
        return false;
    }
    private void inOrder(TreeNode node){
        if(node==null) return;
        inOrder(node.left);
        list.add(node.val);
        inOrder(node.right);
    }
}

测试:

在这里插入图片描述

总结

	对二叉搜索树(BST)算法题来说,插入多半用不到(因为人本身会插好🤣),常用的还得是(中序)遍历,
搜索,删除都用的少。二叉搜索树无非就是二分法的最好体现,将值小的放左边,将值大的放右边。
(常用)将遍历得到的序列 + 其他算法解题。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

假正经的小柴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值