原题链接在这里: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. 约瑟夫环的C语言数组实现

    约瑟夫环问题的具体描述是:设有编号为1,2,……,n的n个(n>0)个人围成一个圈,从第1个人开始报数,报到m时停止报数,报m的人出圈,才从他的下一个人起重新报数,报到m时停止报数,报m的出圈, ...

  2. VSCode eslint校验 tab改为2个空格

    修改:.eslintrc.json

  3. python + Streaming框架的MR实践与优化

    Streaming是Hadoop提供的一个可以使用其他编程语言来进行MR编程的API,它使用Unix标准输入输出作为Hadoop和其他编程语言的开发接口,非常轻便.而开发者可以选择自己擅长的编程语言, ...

  4. EGLImage与纹理

    http://blog.csdn.net/sunnytina/article/details/51895406 Android使用Direct Textures提高glReadPixels.glTex ...

  5. Windos Server Tomcat 双开配置

    Tomcat 双开配置 tomcat_1   server.mxl文件 # 修改端口 <Connector port=" protocol="HTTP/1.1" c ...

  6. classpath是什么

    作用: 告诉Java执行环境,在哪些目录下可以找到您所要执行的Java程序所需要的类或者包(也就是.class文件) JDK 5.0默认就会到JDK的lib目录下寻找Java程序. 如何配置? 有许多 ...

  7. kubernetes liveness readiness

    Liveness Probe(存活探针):用于判断容器是否存货(running状态),如果LivenessProbe探测到容器不健康,则kubelet将杀掉该容器,并根据容器的重启策略做相应的处理.如 ...

  8. 在一个N个整数数组里面,有多个奇数和偶数,设计一个排序算法,令所有的奇数都在左边。

    //在一个N个整数数组里面,有多个奇数和偶数,设计一个排序算法,令所有的奇数都在左边. // 例如: 当输入a = {8,4,1,6,7,4,9,6,4}, // a = {1,7,9,8,4,6,4 ...

  9. Efficient Vector Representation for Documents through Corruption-by Minmin Chen阅读

    关键词: 词向量.文档向量.文档表示 地址:https://openreview.net/forum?id=B1Igu2ogg&noteId=B1Igu2ogg 首先,论文解决的是Word2V ...

  10. Java线程池ExecutorService和CountDownLatch的小例子

    import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java ...