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).

Note: There are at least two nodes in this BST.


题目标签:Binary Search Tree

  这道题目给了我们一个二叉搜索树,其特性为 左 < 根 < 右。让我们找到树中最小的绝对差值,可以存在任意两点中。如果看到二叉搜索树,一定要条件反射性的想起用 inOrder traverse,所有的值是从小到大的排序。这样就很容易找到最小的绝对差了,对于每一个点,和之前那个点比较一下,遍历完树,就可以找到最小的差值。

Java Solution:

Runtime beats 77.33%

完成日期:07/10/2017

关键词:Binary Search Tree

关键点:利用 inOrder traverse 遍历树,所有点的值排序为从小到大

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution
{
int minDiff = Integer.MAX_VALUE;
TreeNode preNode = null; public int getMinimumDifference(TreeNode root)
{
inOrder(root);
return minDiff;
} public void inOrder(TreeNode node)
{
if(node == null)
return; inOrder(node.left); // get the diff between preNode and node
if(preNode != null) // because the first time preNode is null
minDiff = Math.min(minDiff, Math.abs(node.val - preNode.val)); preNode = node; inOrder(node.right);
}
}

参考资料:

http://www.cnblogs.com/grandyang/p/6540165.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)的更多相关文章

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

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

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

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

  3. [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差

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

  4. 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 ...

  5. [LeetCode] Kth Smallest Element in a BST 二叉搜索树中的第K小的元素

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

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

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

  7. 530. Minimum Absolute Difference in BST

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

  8. [LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

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

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

随机推荐

  1. HTML 自动、手动切换轮播 from:金水

    .自动.手动切换轮播 body代码: <div id="tuijian"> <div class="pages" id="p1&qu ...

  2. bookStore案例第一篇【部署开发环境、解决分类模块】

    前言 巩固Servlet+JSP开发模式,做一个比较完整的小项目 成果图 该项目包含了两个部分,前台和后台. 前台用于显示 后台用于管理 该项目可分为5个模块来组成:分类模块,用户模块,图书模块,购买 ...

  3. Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/SpringStruts]]

    今天启动Tomcat时候遇到了这个问题 Failed to start component [StandardEngine[Catalina].StandardHost[localhost].Stan ...

  4. android自定义动画

    前一篇说了实现过程,这次来写一个自己简单实现的3d动画 先来属性声明配置,方便使用xml 文件来定制动画 <!-- 有些类型其实是没必要的,只是实例代码,为了更具有代表性 --> < ...

  5. [js高手之路] html5 canvas系列教程 - arcTo(弧度与二次,三次贝塞尔曲线以及在线工具)

    之前,我写了一个arc函数的用法:[js高手之路] html5 canvas系列教程 - arc绘制曲线图形(曲线,弧线,圆形). arcTo: cxt.arcTo( cx, cy, x2, y2, ...

  6. 详解 HTTPS 移动端对称加密套件优

    近几年,Google.Baidu.Facebook 等互联网巨头大力推行 HTTPS,国内外的大型互联网公司很多也都已启用全站 HTTPS. Google 也推出了针对移动端优化的新型加密套件 Cha ...

  7. oracle数据库知识点

    1.oracle启动后的服务 1. Oracle ORCL VSS Writer Service:Oracle卷映射拷贝写入服务,VSS(Volume Shadow Copy Service)能够让存 ...

  8. JPEG流封装AVI视频

    前言:前几天工作任务,要把JPEG流封装为AVI视频,就找了些AVI文件结构资料和示例代码研究了下,现将学习总结及最终完成的可用代码分享出来,由于本人也是现学现用,如有不恰当或错误之处,欢迎提出! 1 ...

  9. Beautiful Dream hdu3418 (直接做或二分)

    Beautiful Dream Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  10. JFrame的层次结构以及背景颜色设置问题

    JFrame的层次结构: JFrame:窗体,也就是窗口的框架.默认为不可见.不透明的(可以使用isVisible和isOpaque来验证).创建窗口时,最后一步需要调用setVisible(true ...