[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

基础弱到忘了二叉树的traverse怎么写了,还以为要输出到array

[一句话思路]:

先初始化为MAX_VALUE,再按标准格式写

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. traverse函数里面切勿定义变量,会导致重复赋值出错。以前错了没注意
  2. 四则运算的对象也要满足非空not null 的基本条件

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

先初始化为MAX_VALUE,再按标准格式写

[复杂度]:Time complexity: O(n) Space complexity: O(1) 没有额外空间

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

左中右

getMinimumDifference(root.left);

        if (prev != null) {
min = Math.min(min, root.val - prev);
}
prev = root.val; getMinimumDifference(root.right);

[其他解法]:

[Follow Up]:

不是BST,用treeset,复杂度都是lgn,可以取出任何一个元素

[LC给出的题目变变变]:

[代码风格] :

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int min = Integer.MAX_VALUE;
TreeNode prev = null; public int getMinimumDifference(TreeNode root) {
//corner case
if (root == null) {
return min;
}
//in-order traversal
getMinimumDifference(root.left);
if (prev != null) {//only deletable if not null
min = Math.min(min, root.val - prev.val);
}
//refresh the prev
prev = root;
getMinimumDifference(root.right);
//return
return min;
}
}

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

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

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

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

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

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

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

  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. 530. Minimum Absolute Difference in BST

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

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

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

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

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

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

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

随机推荐

  1. Linux history时间用户ip设置

    Linux history时间用户ip设置        在使用linux服务器的时候发生一些不知道谁操作的问题,google一下说history命令可以查看到历史记录,用过之后发现还是不够详细,再g ...

  2. 各大互联网公司2014前端笔试面试题–HTML,CSS篇

    Html篇: 1.你做的页面在哪些流览器测试过?这些浏览器的内核分别是什么? IE: trident内核 Firefox:gecko内核 Safari:webkit内核 Opera:以前是presto ...

  3. RK3288 制作内核开机logo

    安装工具 sudo apt-get install netpbm 1.制作图片 (1).图片为bmp格式 $ convert logo.bmp logo.png $ pngtopnm logo.png ...

  4. sqlserver2008事务日志已满

    --将数据库设为简单模式.日志文件自动断开. alter database CustomerInfoProject set recovery simple --查看日志文件状况  use Custom ...

  5. ESP8266编译过程探索

    首先看到的是为什么SDK里有一个libmain.a这样的库. 编译之后才发现原平是这样子:main函数(如果有的话)会调用我们user_init函数.user_rf_cal_sector_set函数, ...

  6. 求助OPC Opc.IDiscovery m_discovery = new OpcCom.ServerEnumerator();

    各位大哥们,大家好,在此请教各位一个问题,谢谢大家.我在vs2010中引用了OpcNetApi.dll和OpcNetCom.dll并且加入了using Opc;using Opc.Da;using O ...

  7. Spring Boot Maven 打包可执行Jar文件

    本文转载自:http://blog.csdn.net/smilecall/article/details/56288972 Maven pom.xml 必须包含 <packaging>ja ...

  8. (转)Inno Setup入门(二十二)——Inno Setup类参考(8)

    本文转载自:http://blog.csdn.net/yushanddddfenghailin/article/details/17268473 列表框 列表框(ListBox)是Windows应用程 ...

  9. [Java]一步一步学 Web

    部分内容来自:http://www.cnblogs.com/jinzhenshui/p/3345895.html Java 中的锁写作 synchronized (this) {} .net 中的锁写 ...

  10. Type-C潮流下 如何衡量一款数据线好坏?

    不少新一代手机开始支持Type-C接口,比如乐视.PPTV.努比亚Z11.小米4C和三星Note7等.和普通Micro USB相比,Type-C数据线因为正反插的关系对品质要求更高,不然随时有短路烧毁 ...