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. A workaround to change shared memory size for Docker containers in AWS ECS

    Issue Because of not supporting to specify the following docker run parameter, containers in ECS can ...

  2. Oracle11g静默安装dbca,netca报错处理--直接跟换操作系统

    最近给一个客户安装oracle 11gr2 概述: 操作系统:linux 32位操作系统 [oracle@nbsrfx response]$ uname -aLinux nbsrfx 2.6.32-5 ...

  3. scrapy初试水 day02(正则提取)

    1.处理方式 法一 通过HtmlXPathSelectorimport scrapyfrom scrapy.selector import HtmlXPathSelectorclass DmozSpi ...

  4. Mysql实现企业级日志管理、备份与恢复实战

    背景 随着业务的发展,公司业务和规模不断扩大,网站积累了大量的用户信息和数据,对于一家互联网公司来说,用户和业务数据是根基.一旦公司的数据错乱或者丢失,对于互联网公司而言就等于说是灭顶之灾,为防止系统 ...

  5. 实践作业1:测试管理工具实践 Day4

    由小组吴辉同学和王俊杰同学负责撰写使用手册,详细记录了环境配置完整过程,以及从软件登陆开始,创建一个测试计划,创建新版本,创建测试用例集,分配测试用例到测试计划,关联到测试用例这一系列完整过程. 刘思 ...

  6. WampSever 安装问题解析

    鉴于有些电脑安装wampsever出现的几种问题 [1] 2.2版本问题 这里端口配置正确,本地服务器没有开启(也就是80端口没有占用) 还是出现这种情况 也就是Apache service 和 PH ...

  7. Scala入门系列(十):函数式编程之集合操作

    1. Scala的集合体系结构 Scala中的集合体系主要包括(结构跟Java相似): Iterable(所有集合trait的根trait) Seq(Range.ArrayBuffer.List等) ...

  8. Linux 使用tcpdump观察arp通信过程

    ARP协议简介: ARP协议能实现任意网络层地址到任意物理地址的转换,此次讨论从IP地址到以太网地址(MAC地址)的转换.其工作原理是:主机向自己所在的网络广播一个ARP请求,该请求包含目标机器的网络 ...

  9. Java进阶(七)正确理解Thread Local的原理与适用场景

    原创文章,始自发作者个人博客,转载请务必将下面这段话置于文章开头处(保留超链接). 本文转发自技术世界,原文链接 http://www.jasongj.com/java/threadlocal/ Th ...

  10. javaWeb中URLEncoder.encode空格问题

    近期开发一个在线坐席的功能.发现推送的消息中空格变成了+ .查询发现URLEncoder.encode的问题.曾经用的时候也没注意过,解决的方法网上是对URLEncoder.encode的之后的字符串 ...