[抄题]:

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. IIS7、IIS7.5中应用程序池最优配置方案

    https://www.cnblogs.com/xinaixia/p/5945678.html 找到Web站点对应的应用程序池,“应用程序池” → 找到对应的“应用程序池” → 右键“高级设置...” ...

  2. Regexper:牛逼的 JavaScript 正则可视化工具

    RequireJS Optimizer 是 RequireJS 自带的前端优化工具,可以对 RequireJS 项目中的 JavaScript & CSS 代码使用 UglifyJS 或者 C ...

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

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

  4. C语言(C99标准)在结构体的初始化上与C++的区别

    C++中由于有构造函数的概念,所以很多时候初始化工作能够很方便地进行,而且由于C++标准库中有很多实用类(往往是类模板),现代C++能十分容易地编写. 比如现在要构造一个类Object,包含两个字段, ...

  5. python TypeError: unsupported operand type(s) for +: 'int' and 'str' [closed]

    TypeError: unsupported operand type(s) for +: 'int' and 'str' [closed] sql="insert into auto_tr ...

  6. activemq的启动方式

    一.简介:ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台 ...

  7. Linux的内存管理机制

    原文作者:技术成就梦想 链接:http://ixdba.blog.51cto.com/2895551/541355 一 物理内存和虚拟内存          我们知道,直接从物理内存读写数据要比从硬盘 ...

  8. 初探asciinema

    在学习c++的时候想到so库注入到某些程序中,网上查了一些资料,发现了一些比较好玩的程序,分享一下. 主要可以对操作的进行视频回放,类似堡垒机的功能 安装: pip3 install asciinem ...

  9. Julia - 复合表达式

    复合表达式是用一个表达式按照顺序对一系列子表达式求值,并返回最后一个子表达式的值 有两种方法:begin 块和 “;” 链 begin 块 begin 块的多行写法 julia> a = beg ...

  10. lunix,命令集锦

    1. ls命令 ls命令是列出目录内容(List Directory Contents)的意思.运行它就是列出文件夹里的内容,可能是文件也可能是文件夹. ? 1 2 3 4 5 6 7 root@te ...