原题链接在这里:https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description

题目:

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 Tree Inorder Traversal, 由于是BST, 所以是asending的, 取出最小difference.

Time Complexity: O(n). Space: O(logn), stack space.

AC Java:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
int min = Integer.MAX_VALUE;
Integer pre = null;
public int getMinimumDifference(TreeNode root) {
if(root == null){
return min;
}
getMinimumDifference(root.left); if(pre != null){
min = Math.min(min, root.val-pre);
}
pre = root.val; getMinimumDifference(root.right);
return min;
}
}

如果不是BST的话可以借助于TreeSet<Integer> ts, 对于每一个node, 找出node.val在ts中的floor和ceil, 计算minimum difference. 再把node 本身的val加到ts中.

Time Complexity: O(nlogn). Space: O(n), ts size.

AC Java:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
int min = Integer.MAX_VALUE;
TreeSet<Integer> ts = new TreeSet<Integer>(); public int getMinimumDifference(TreeNode root) {
if(root == null){
return min;
} if(!ts.isEmpty()){
if(ts.floor(root.val) != null){
min = Math.min(min, root.val-ts.floor(root.val));
}
if(ts.ceiling(root.val) != null){
min = Math.min(min, ts.ceiling(root.val)-root.val);
}
}
ts.add(root.val); getMinimumDifference(root.left);
getMinimumDifference(root.right);
return min;
}
}

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

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

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

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

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

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

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

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

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

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

  6. [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 ...

  7. C#LeetCode刷题之#530-二叉搜索树的最小绝对差(Minimum Absolute Difference in BST)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4123 访问. 给定一个所有节点为非负值的二叉搜索树,求树中任意两 ...

  8. [Swift]LeetCode530. 二叉搜索树的最小绝对差 | Minimum Absolute Difference in BST

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

  9. Binary Search Tree-530. Minimum Absolute Difference in BST

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

随机推荐

  1. Centos(Yum源更改)

    第一步:备份你的原镜像文件,以免出错后可以恢复. [root@openstack yum.repos.d]#mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum. ...

  2. Python学习进程(9)序列

    序列是Python中最基本的数据结构.     (1)序列简介: 序列中的每个元素都分配一个数字标明它的位置或索引,第一个索引是0,第二个索引是1,依此类推.序列都可以进行的操作包括索引,切片,加,乘 ...

  3. 主攻ASP.NET.4.5.1 MVC5.0之重生:政府行政网站常用友情链接跳转javascript[干货分享]

    <!-----------------------------------> <script language="JavaScript" type="t ...

  4. 主攻ASP.NET.4.5.1 MVC5.0之重生:空地搭建一个包含 Ninject框架 项目

    1.创建一个空白解决方案 2.添加一个类库 名称为XXX.Domain 3.添加一个ASP.MVC 名称为XXX.WebUI 4.选着空模版,勾选MVC核心引用 5.添加单元测试项目XXX.UntiT ...

  5. 跨平台移动开发 Xuijs超轻量级的框架Style CSS属性用法

    PhoneGap里面推荐使用的超轻量级的框架 Style CSS属性用法 设置css属性:setstyle 通过ID设置css属性 x$('#top1').setStyle('color', '#DB ...

  6. 【计算机网络】OSI模型,TCPIP模型

    今天给大家分享的是两种模型的主要区别,夜视比较容易混淆的地方.我尽力用图形的方式来说问题,这样比较好理解~ (PS:画图真的不会,正在认真学,希望多多包含:)) 一.二者的模型对比 (这个图有点丑.. ...

  7. struts2中常用配置

    1.Post提交乱码问题,如果编码采用的是utf-8,那么默认不需要自己处理,因为其默认的常量配置文件就是处理UTF-8的 这个常量值只处理POST提交,get如果乱码还得自己写拦截器处理,一般只要页 ...

  8. Luogu-4022 [CTSC2012]熟悉的文章

    广义后缀自动机+DP 对于作文库建出广义后缀自动机,广义自动机就是在每次添加一个字符串之前把\(last=0\),然后正常添加就好了 对于每个询问串,预处理出每个位置\(i\)能向前匹配的最长长度\( ...

  9. java深入探究10-文件上传组件FileUpload,邮件开发

    1.文件上传组件FileUpload 1)java提供了文件上传的工具包 需要引入:commons-fileupload-1.2.1.jar(文件上床组件核心包) commons-oi-1.4(封装了 ...

  10. socket io 记得flush

    public class Client { public static void main(String args[]) throws Exception { //为了简单起见,所有的异常都直接往外抛 ...