[抄题]:

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. centos 限制只能访问某个目录的php文件

    vi /etc/php.ini #编辑 open_basedir = .:/tmp/ #在380行 设置表示允许访问当前目录(即PHP脚本文件所在之目录)和/tmp/目录,可以防止php木马跨站,如果 ...

  2. Linux history时间用户ip设置

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

  3. Request.UrlReferrer详解

    使用前需要进行判断: if (Request != null && Request.UrlReferrer != null && Request.UrlReferrer ...

  4. mysql binlog_format row and Statement 比较

    两种模式的对比: Statement 优点 历史悠久,技术成熟: 产生的 binlog 文件较小: binlog 中包含了所有数据库修改信息,可以据此来审核数据库的安全等情况: binlog 可以用于 ...

  5. 封装与继承(PHP学习)

    什么是封装? 答:封装时不知道内部构造,对外部只展现功能的这种行为.例如:收音机,你不知道收音机内部的构造,但是你知道收音机是能用来听广播的. 在PHP中,封装是,不对外公布,属性和方法,这些属性和方 ...

  6. 解决win下无法ping通VM虚拟机CentOS系统的方法

    事情描述:公司迁新址,电脑带过去之后,用xshell连接vm的centos系统老是连接失败,然后考虑到公司迁新址这个情况,我首先怀疑是ip的问题,然后在vm中执行ifconfig找到centos的ip ...

  7. 减少TIME_WAIT时间的优化配置

    减少TIME_WAIT时间的优化配置 建立TCP需要三次握手才能建立,而断开连接则需要四次握手.整个过程如下图所示: net.ipv4.tcp_max_syn_backlog=8192 增加TCP S ...

  8. Jq将字符串复制粘贴到剪贴板

      第一种: 自己测试时 只适合于input 和textarea 但是针对于其他标签的复制就不能用了.代码如下: <!DOCTYPE html> <html> <head ...

  9. supervisor 管理

    Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启.它是通过fork/exec的方式把这些被管理的进程 ...

  10. 字体相关CSS属性介绍

    font-family 字体系列. font-family可以把多个字体名称作为一个“回退”系统来保存.如果浏览器不支持第一个字体,则会尝试下一个.浏览器会使用它可识别的第一个值. 简单实例: bod ...