首页 > 其他分享 >leetcode-783-easy

leetcode-783-easy

时间:2023-02-15 21:33:22浏览次数:26  
标签:return 783 int min minDiffInBSTRec easy minDiffInBSTList root leetcode

Minimum Distancd Between Nodes

Given the root of a Binary Search Tree (BST), return the minimum difference between the values of any two different nodes in the tree.

Example 1:

Input: root = [4,2,6,1,3]
Output: 1
Example 2:

Input: root = [1,0,48,null,null,12,49]
Output: 1
Constraints:

The number of nodes in the tree is in the range [2, 100].
0 <= Node.val <= 105
Note: This question is the same as 530: https://leetcode.com/problems/minimum-absolute-difference-in-bst/

思路一:先用中序遍历收集节点,然后依次对比

    List<Integer> minDiffInBSTList = new ArrayList<>();
    public int minDiffInBST(TreeNode root) {
        minDiffInBSTRec(root);
        int min = Integer.MAX_VALUE;
        for (int i = 1; i < minDiffInBSTList.size(); i++) {
            min = Math.min(Math.abs(minDiffInBSTList.get(i) - minDiffInBSTList.get(i - 1)), min);
        }

        return min;
    }

    public void minDiffInBSTRec(TreeNode root) {
        if (root == null) {
            return;
        }

        minDiffInBST(root.left);
        minDiffInBSTList.add(root.val);
        minDiffInBST(root.right);
    }

思路二:看了一下官方解答,发现用中序遍历记录前一个值,依次对比就行

    private  int minDiffInBSTVal = Integer.MAX_VALUE;
    private  int minDiffInBSTPre = -1;
    public  int minDiffInBST(TreeNode root) {
        minDiffInBSTRec(root);

        return minDiffInBSTVal;
    }
    public  void minDiffInBSTRec(TreeNode root) {
        if (root == null) {
            return;
        }

        minDiffInBSTRec(root.left);
        if (minDiffInBSTPre == -1) {
            minDiffInBSTPre = root.val;
        } else {
            minDiffInBSTVal = Math.min(minDiffInBSTVal, root.val - minDiffInBSTPre);
            minDiffInBSTPre = root.val;
        }
        minDiffInBSTRec(root.right);
    }

标签:return,783,int,min,minDiffInBSTRec,easy,minDiffInBSTList,root,leetcode
From: https://www.cnblogs.com/iyiluo/p/17124780.html

相关文章

  • leetcode-824-easy
    GoatLatinYouaregivenastringsentencethatconsistofwordsseparatedbyspaces.Eachwordconsistsoflowercaseanduppercaselettersonly.Wewouldlik......
  • 【LeetCode栈与队列#05】滑动窗口最大值
    滑动窗口最大值力扣题目链接(opensnewwindow)给定一个数组nums,有一个大小为k的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的k个数字。......
  • [Leetcode]435. 无重叠区间
    435.无重叠区间给定一个区间的集合 intervals ,其中intervals[i]=[starti,endi] 。返回需要移除区间的最小数量,使剩余区间互不重叠 。示例1:输入:intervals......
  • 【算法训练营day44】完全背包基础 LeetCode518. 零钱兑换II LeetCode377. 组合总和IV
    LeetCode518.零钱兑换II题目链接:518.零钱兑换II独上高楼,望尽天涯路组合问题和完全背包的混合应用,感觉脑中模拟的不是很清晰,但是靠着背包问题的代码技巧和模板就能比较......
  • 【算法训练营day43】LeetCode1049. 最后一块石头的重量II LeetCode494. 目标和 LeetCo
    LeetCode1049.最后一块石头的重量II题目链接:1049.最后一块石头的重量II独上高楼,望尽天涯路一开始还是没有想到怎么转化成01背包问题,所以直接看题解找思路慕然回首,灯......
  • vue3之异步组件defineAsyncComponent 使用无效?
    原文地址:我的稀土掘金介绍:defineAsyncComponent用于拆分应用为更小的块,并仅在需要时再从服务器加载相关组件官网案例<scriptsetup>import{defineAsyncComponent......
  • 【LeetCode队列#04】逆波兰表达式求值(仍然是经典的栈操作)
    逆波兰表达式求值力扣题目链接(opensnewwindow)根据逆波兰表示法,求表达式的值。有效的运算符包括+,-,*,/。每个运算对象可以是整数,也可以是另一个逆波兰表达......
  • leetcode-数据结构与算法
    第0001题:求两数之和leetcode对应题号:1力扣-原题链接:[请点击此处](https://leetcode.cn/problems/two-sum/"请点击此处")方法一思路暴力破解法,时间复杂度是\(O(n......
  • leetcode:题目1-
    第1题,对应leetcode题目编号:一、题目:xxx1、原题-力扣链接:请点击此处二、思路+代码1、方法一:一、思路二、代码statussList_merge3(mySList*pa,mySList*pb){ if......
  • leetcode - 1250 检查好数组
    检查好数组题目给你一个正整数数组nums,你需要从中任选一些子集,然后将子集中每一个数乘以一个任意整数,并求出他们的和。假如该和结果为1,那么原数组就是一个「好数组」......