Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.

Example:

Input:

   1
\
3
/
2 Output:
1 Explanation:
The minimum absolute difference is 1, which is the difference between 2 and 1 (or between 2 and 3).

题目要求找到二叉搜索树中,节点之间最小的差值。我们知道二叉搜索树的中序遍历为有序的,最小的差值就出现在相邻元素之间的差值。

class Solution {
int min = Integer.MAX_VALUE;
Integer prev = null; //记录前一个节点,可以定义为TreeNode
public int getMinimumDifference(TreeNode root) {
if(root == null) return min;
getMinimumDifference(root.left);
if(prev != null)
min = Math.min(min,root.val - prev);
prev = root.val;
getMinimumDifference(root.right);
return min;
}
}

530. Minimum Absolute Difference in BST的更多相关文章

  1. 51. leetcode 530. Minimum Absolute Difference in BST

    530. Minimum Absolute Difference in BST Given a binary search tree with non-negative values, find th ...

  2. 【leetcode_easy】530. Minimum Absolute Difference in BST

    problem 530. Minimum Absolute Difference in BST 参考 1. Leetcode_easy_530. Minimum Absolute Difference ...

  3. LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  4. [LeetCode&Python] Problem 530. Minimum Absolute Difference in BST

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  5. 530.Minimum Absolute Difference in BST 二叉搜索树中的最小差的绝对值

    [抄题]: Given a binary search tree with non-negative values, find the minimum absolute difference betw ...

  6. 【LeetCode】530. Minimum Absolute Difference in BST 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  7. leetcode 783. Minimum Distance Between BST Nodes 以及同样的题目 530. Minimum Absolute Difference in BST

    Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...

  8. 【easy】530. Minimum Absolute Difference in BST

    找BST树中节点之间的最小差值. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...

  9. 530 Minimum Absolute Difference in BST 二叉搜索树的最小绝对差

    给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值.示例 :输入:   1    \     3    /   2输出:1解释:最小绝对差为1,其中 2 和 1 的差的绝对值为 ...

随机推荐

  1. Activity讲解

    Activity Activity 是 Android 应用的重要组成单元之一(另外三个是 Service.BroadcastReceiver 和 ContentProvider),而 Activit ...

  2. android stdio 快捷键

    俗话说工欲善其事必先利其器,先熟悉快捷键是很有必要的                 IDE   按键                                                 ...

  3. c#游戏进程杀手

    我认为写博客还是比较重要的,特别是短时间写出一个含有新知识点的软件.这样总结下这次编程经验和再捋顺一下这次编程思路.首先来谈谈为什么想做这个小程序,一是感觉自己太贪玩想控制一下,二是也锻炼下自己的编程 ...

  4. Error when sending message to topic test with key: null, value: 2 bytes with error: (org.apache.kafka.clients.producer.internals.ErrorLoggingCallback)

    windows下使用kafka遇到这个问题: Error when sending message to topic test with key: null, value: 2 bytes with ...

  5. 如何使用python将MySQL中的查询结果导出为Excel----xlwt的使用

    如何在MySQL中执行的一条查询语句结果导出为Excel? 一.可选方法 1.使用sql yog等远程登录,执行查询语句并导出结果集为Excel 适用于较简单的查询结果集的导出 如果需要多个SQL语句 ...

  6. window下phpstudy的nginx配置虚拟主机

    由于很长时间没有配置Apache,虽然说知道怎么配置nginx,但是还是花费了一些时间这次记下来下次直接用 在其他选项文件菜单中->打开配置文件->选择vhosts-conf nginx的 ...

  7. 浅谈php的优缺点

    一.优点 1. 跨平台,性能优越,跟Linux/Unix结合别跟Windows结合性能强45%,并且和很多免费的平台结合非常省钱,比如LAMP(Linux /Apache/Mysql/PHP)或者FA ...

  8. Python函数篇(5)-装饰器及实例讲解

    1.装饰器的概念   装饰器本质上就是一个函数,主要是为其他的函数添加附加的功能,装饰器的原则有以下两个: 装饰器不能修改被修饰函数的源代码 装饰器不能修改被修改函数的调用方式   装饰器可以简单的理 ...

  9. 【luogu1220】关路灯

    https://www.luogu.org/problem/show?pid=1220 假如当前老张在a处跑去关掉b处的路灯,那么a与b之间的路灯都可以顺手关掉.因此每一时刻关掉的路灯必定是连续的. ...

  10. 自学OpenCV时遇到的一些错误(捂脸ing,当年确实好多不懂...)

    6.8/14 opencv环境配置好了, 属性也设置对了, 但是使用VideoCapture类的时候, 还是出错.  右键可以查看其定义, 但是就是 出错.   漏掉了#include "s ...