原题链接在这里: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. Informatica can bind a LONG value only for insert into a LONG column Oracle

    Informatica实现etl同步表数据信息时 报: Severity Timestamp Node Thread Message Code Message ERROR 2016/8/8 17:32 ...

  2. 区块链入门级别认知(blockchain)

    区块链入门级别认知(blockchain) 前言:今天参加了迅雷关于区块链的大会,学习和感受总结一下 之前的认知在:几个混迹互联网圈关于区块链 耳熟能详的 热词 “比特币” “区块链” “挖矿” ,知 ...

  3. curl扩展代码

    /** * * curl 支持post * @param string $base_url 基础链接 * @param array $query_data 需要请求的数据 * @param strin ...

  4. MySQL数据库基本操作(三)

    MySQL补充: mysql是关系型数据库,关系数据库,是建立在关系模型基础上的数据库,现实世界中的各种实体,以及实体之间的各种联系,均用关系模型(table)来表示.#关系模型就是指二维表格模型,因 ...

  5. React-Native Listview组件用法详解

    ListView作为React Native的核心组件,用于高效地显示一个可以垂直滚动的变化的数据列表.其中最重要的属性之一是DataSource,列表依赖的数据源,用于实例化一个ListView对象 ...

  6. INSPIRED启示录 读书笔记 - 第16章 市场调研

    常用的市场调研工具和方法 用户调查:第一,设计调查问卷需要技巧和经验,不是一件容易的事:第二,调查结果为获得解决方案提供了一要途径,但不是解决方案本身 产品使用分析:使用分析工具记录用户使用产品的行为 ...

  7. 【P2564】生日礼物(单调队列)

    这个题看上去状态比较多,实际上由于题目的输出需要,又因为是一个线性的结构,所以我们可以有一些操作. 这么想,如果我们有了一个满足条件的区间,此时我们缩减左端点,然后判断此时是否还是满足,满足就继续缩减 ...

  8. git 里面遇到的问题

    第一步:建立git仓库(本地) cd到你的本地项目根目录下,执行git命令 git init 第二步:将项目的所有文件添加到仓库中 git add . 如果想添加某个特定的文件,只需把.换成特定的文件 ...

  9. nova Evacuate

    作用:当一个 node down 掉后,在新的 node 上根据其 DB 中保存的信息重新 build down node 上虚机.这个往往在虚机 HA 方案中用到.它尽可能地将原来的虚机在新的主机上 ...

  10. shell基础之书写需要用到的小工具

    一.简单介绍 下面介绍几个可能不太常用但是面试题经常会用用到的小工具: 命令:cut 作用:截取某一个字段 '-d' 后面跟分隔字符,把字段分割为若干个区间. '-c' 后面接的是第几个字符,也可以是 ...