day18 | 530.二叉搜索树的最小绝对差 501.二叉搜索树中的众数 236. 二叉树的最近公共祖先

代码随想录算法训练营第18天| 530.二叉搜索树的最小绝对差 501.二叉搜索树中的众数 236. 二叉树的最近公共祖先

Leetcode 530.二叉搜索树的最小绝对差
题目链接:https://leetcode.cn/problems/minimum-absolute-difference-in-bst/description/
题目描述:给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值

差值是一个正数,其数值等于两值之差的绝对值。

示例 1:

img

输入:root = [4,2,6,1,3]
输出:1

示例 2:

img

输入:root = [1,0,48,null,null,12,49]
输出:1
思路:

递归

代码:递归
/**
 * 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 {
    // 声明遍历的节点的前一个节点
    TreeNode pre;
    // 寻找最小值,声明最大值
    int MaxValue = Integer.MAX_VALUE;
    public int getMinimumDifference(TreeNode root) {
        // 双指针
        if(root == null) return 0;
        getMinValue(root);
        return MaxValue;
    }
    // 递归

    // 确定递归参数和返回值
    public void getMinValue(TreeNode root){
        if(root == null) return;
        // 中序遍历

        // 左
        getMinValue(root.left);
        // 中
        if(pre != null){
            MaxValue = Math.min(MaxValue,root.val - pre.val);
        }
        pre = root;
        // 右
        getMinValue(root.right);

    }
}
Leetcode 501.二叉搜索树中的众数
题目链接:https://leetcode.cn/problems/find-mode-in-binary-search-tree/description/
题目描述:给你一个含重复值的二叉搜索树(BST)的根节点 root ,找出并返回 BST 中的所有 众数(即,出现频率最高的元素)。

如果树中有不止一个众数,可以按 任意顺序 返回。

假定 BST 满足如下定义:

  • 结点左子树中所含节点的值 小于等于 当前节点的值
  • 结点右子树中所含节点的值 大于等于 当前节点的值
  • 左子树和右子树都是二叉搜索树

示例 1:

img

输入:root = [1,null,2,2]
输出:[2]

示例 2:

输入:root = [0]
输出:[0]

提示:

  • 树中节点的数目在范围 [1, 104]
  • -105 <= Node.val <= 105

**进阶:**你可以不使用额外的空间吗?(假设由递归产生的隐式调用栈的开销不被计算在内)

思路:

1、递归

2、迭代

代码1:递归
/**
 * 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 {
    // pre 记录当前的节点的前一个节点
    ArrayList<Integer> resList;
    TreeNode pre ;
    int count ;
    int maxCount ;
    public int[] findMode(TreeNode root) {
        // 声明变量
        resList = new ArrayList<>();
        maxCount = 0;
        count = 0;
        pre = null;
        findMode1(root);
        int[] res = new int[resList.size()];
        for(int i = 0;i<resList.size();i++){
            res[i] = resList.get(i);
        }
        return res;
        // 利用中序遍历
    }
    // 确定参数和返回值
    public void findMode1(TreeNode root){
       
        // 终止条件
        if(root == null) return;
        // 左
        findMode1(root.left);
        // 中
         int rootValue = root.val;
        if(pre == null || rootValue != pre.val){
            count = 1;
        }else{
            count++;
        }
        if(maxCount < count){
            // 清除节点
            resList.clear();
            resList.add(rootValue);
            maxCount = count;
        }else if(count == maxCount){
            resList.add(rootValue);
        }
        pre = root;
        // 右
        findMode1(root.right);
    }

}
代码2:迭代
/**
 * 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 int[] findMode(TreeNode root) {
        // 迭代法
        TreeNode pre = null;
        Stack<TreeNode> stack = new Stack<>();
        List<Integer> res =new ArrayList<>();
        int maxCount = 0;
        int count = 0;
        TreeNode cur = root;
        // 遍历root
        while(cur!=null || !stack.isEmpty()){
            if(cur!= null){
                stack.push(cur);
                cur = cur.left;
            }else{
                cur = stack.pop();

                // 计数
                if(pre == null || cur.val != pre.val){
                    count = 1;
                }else if(pre.val == cur.val){
                    count ++;
                }

                // 更新结果
                if(count > maxCount){
                    maxCount =count;
                    res.clear();
                    res.add(cur.val);
                }else if(count == maxCount){
                    res.add(cur.val);
                }

                // 移动
                pre = cur;

                // 右
                cur = cur.right;

            }
        }
        return res.stream().mapToInt(Integer::intValue).toArray();
        
    }
}
// result.stream().mapToInt(Integer::intValue).toArray();

// int[] arr1 = list1.stream().mapToInt(Integer::valueOf).toArray();
// 想要转换成int[]类型,就得先转成IntStream。
// 这里就通过mapToInt()把Stream<Integer>调用Integer::valueOf来转成IntStream
// 而IntStream中默认toArray()转成int[]。
总结:
Leetcode 236. 二叉树的最近公共祖先
题目链接:https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/description/
题目描述:给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。

百度百科中最近公共祖先的定义为:“对于有根树 T 的两个节点 p、q,最近公共祖先表示为一个节点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”

示例 1:

img

输入:root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
输出:3
解释:节点 5 和节点 1 的最近公共祖先是节点 3 。

示例 2:

img

输入:root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
输出:5
解释:节点 5 和节点 4 的最近公共祖先是节点 5 。因为根据定义最近公共祖先节点可以为节点本身。

示例 3:

输入:root = [1,2], p = 1, q = 2
输出:1

提示:

  • 树中节点数目在范围 [2, 105] 内。
  • -109 <= Node.val <= 109
  • 所有 Node.val 互不相同
  • p != q
  • pq 均存在于给定的二叉树中。
思路:

1、递归

代码1:递归
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        // 终止条件
        if(root == q || root == p || root == null) return root;
        // 左
        TreeNode left =  lowestCommonAncestor(root.left , p , q);
        // 右
        TreeNode right = lowestCommonAncestor(root.right , p , q);
        // 中
        if(left == null && right != null) return right;
        else if(left != null && right == null) return left;
        else if(left == null && right == null) return null;
        else{
            return root;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值