首页 > 其他分享 >LeetCode 530.二叉搜索树的最小绝对值差

LeetCode 530.二叉搜索树的最小绝对值差

时间:2023-04-10 14:08:50浏览次数:57  
标签:right TreeNode val int 二叉 530 root LeetCode left

1.题目:

给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 。

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


示例 1:

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

来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/minimum-absolute-difference-in-bst 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。



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 getMinimumDifference(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        preorder(root,result);
        int max = Integer.MAX_VALUE;
        for(int i=0; i<result.size(); i++){
            for(int j=i+1; j<result.size();j++){
                if(Math.abs(result.get(i)-result.get(j))<=max){
                    max=Math.abs(result.get(i)-result.get(j));
                }
            }
        }
       
        return max;
    }
    public void preorder(TreeNode root,List<Integer> result){
        if(root==null) return;
        result.add(root.val);
        preorder(root.left,result);
        preorder(root.right,result);
    }
}


方法二:

边中序遍历,边用指针

/**
 * 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 = null;//用来记录前一个结点的值
    int max = Integer.MAX_VALUE;//用来作比较,记录最小值
    public int getMinimumDifference(TreeNode root) {
        if(root==null) return 0;//注意这里也要返回,因为返回值是整数
        inorderPlus(root);
        return max;

    }
    public void inorderPlus(TreeNode root){
        if(root==null) return;
        inorderPlus(root.left);//左
        if(pre!=null){//中
            max=Math.min(max,Math.abs(root.val-pre.val));
        }
        pre=root;//移动指针
        inorderPlus(root.right);//右

    }
}



标签:right,TreeNode,val,int,二叉,530,root,LeetCode,left
From: https://blog.51cto.com/u_15806469/6180631

相关文章

  • LeetCode习题——x 的平方根(二分查找)
    ###x的平方根力扣链接:[x的平方根](https://leetcode.cn/problems/sqrtx/)####题目>给你一个非负整数x,计算并返回x的算术平方根。>>由于返回类型是整数,结果只保留整数部分,小数部分将被舍去。>>注意:不允许使用任何内置指数函数和算符,例如pow(x,0.5)或者x*......
  • 判断二叉树是否对称
    递归遍历recursion(root1,root2){if(root1==null&&root2== null){returnture;}if(root1==null||root2==null||root1.val!=root2.val)returnfalse;returenrecursion(root1.left,root2.right)&&recursion(root2.left,root2.right);......
  • 搜索二叉树转换成双向链表
    搜索二叉树:每个节点的左子树的值都小于当前节点,右子树的节点值都大于当前节点。其中序遍历就是一个有序的序列转化成双向链表,需要记录一下头节点,和前一个节点,将前一个节点和当前节点相连preheadconvert(pRoot){if(pRoot==null)returnnull;convert(pRoot.left);......
  • 98. 验证二叉搜索树
    给你一个二叉树的根节点root,判断其是否是一个有效的二叉搜索树。有效二叉搜索树定义如下:节点的左子树只包含小于当前节点的数。节点的右子树只包含大于当前节点的数。所有左子树和右子树自身必须也是二叉搜索树。classSolution{public:longlongmaxVal=LON......
  • 700. 二叉搜索树中的搜索
    给定二叉搜索树(BST)的根节点root和一个整数值val。你需要在BST中找到节点值等于val的节点。返回以该节点为根的子树。如果节点不存在,则返回null。classSolution{public:TreeNode*searchBST(TreeNode*root,intval){if(root==nullptr)returnnu......
  • 二叉树的最大深度,二叉树是否存在路径和为某值的路径
    递归的方法遍历二叉树最大深度:fun(root){if(root==null){ return0;}return(Max(fun(root.left),fun(root.right))+1);}和为某值fun(root,sum){if(root==null){returnfalse;}if(root.left==null&&root.right......
  • LeetCode 98.验证二叉搜索树
    1.题目:给你一个二叉树的根节点root,判断其是否是一个有效的二叉搜索树。有效二叉搜索树定义如下:节点的左子树只包含小于当前节点的数。节点的右子树只包含大于当前节点的数。所有左子树和右子树自身必须也是二叉搜索树。示例1:输入:root=[2,1,3]输出:true来源:力扣(LeetCode......
  • 二叉树层序遍历和之字遍历
    1.用一个队列记录当前层的节点,然后一个个取出,取出的同时将取出节点的儿子节点加入到队列中。2.之字遍历则需要一个标志为将行进行翻转ArrayList<Integer>(ArrayList<Integer>())res;flag=true;//实现奇数行翻转,偶数行不翻转Queuetemp;temp.offer(head)while(temp!=nul......
  • 判断完全二叉树
    1.题目链接天梯赛真题--是否是完全二叉搜索树2.根据节点编号判断(better)借鉴自这里规定根节点的编号为\(1\),左孩子节点编号为\(left\),右孩子节点编号为\(right\),父节点的节点编号为\(fa\),则有:\(left=fa<<1\)\(right=fa<<1|1\)由于完全二叉树的节点编号......
  • Leetcode(剑指offer专项训练)——DFS/BFS专项(2)
    课程顺序题目现在总共有numCourses 门课需要选,记为 0 到 numCourses-1。给定一个数组 prerequisites,它的每一个元素 prerequisites[i] 表示两门课程之间的先修顺序。 例如 prerequisites[i]=[ai,bi] 表示想要学习课程ai ,需要先完成课程bi 。请根据给出的......