530.Minimum Absolute Difference in BST 二叉搜索树中的最小差的绝对值
[抄题]:
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,再按标准格式写
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- traverse函数里面切勿定义变量,会导致重复赋值出错。以前错了没注意
- 四则运算的对象也要满足非空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 二叉搜索树中的最小差的绝对值的更多相关文章
- 530 Minimum Absolute Difference in BST 二叉搜索树的最小绝对差
给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值.示例 :输入: 1 \ 3 / 2输出:1解释:最小绝对差为1,其中 2 和 1 的差的绝对值为 ...
- [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- 【leetcode_easy】530. Minimum Absolute Difference in BST
problem 530. Minimum Absolute Difference in BST 参考 1. Leetcode_easy_530. Minimum Absolute Difference ...
- 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 ...
- 530. Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- 【LeetCode】530. Minimum Absolute Difference in BST 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- [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 ...
- [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 ...
随机推荐
- linux-docker安装
https://yeasy.gitbooks.io/docker_practice/content/install/centos.html
- MySQL 性能优化技巧
原文地址:MySQL 性能优化技巧 博客地址:http://www.extlight.com 一.背景 最近公司项目添加新功能,上线后发现有些功能的列表查询时间很久.原因是新功能用到旧功能的接口,而这 ...
- 5.Python使用最新爬虫工具requests-html
1.安装,在命令行输入:pip install requests-html,安装成功后,在Pycharm引入即可. 2.代码如下所示: from requests_html import HTMLSe ...
- java的日期
直接看例子: import java.text.DateFormatSymbols; import java.util.Calendar; import java.util.GregorianCale ...
- GNU Radio 入门培训
1. GNU Radio介绍 1.1 什么是GNU Radio GNU Radio是一个完全开源的软件无线电结构平台,它可以用来设计和仿真,也可以用来连接真实的无线电系统.GNU Radio是一个高度 ...
- python接口自动化22-签名(signature)鉴权(authentication)之加密(HEX、MD5、HMAC-SHA256)
前言 开放的接口为了避免被别人乱调用,浪费服务器资源,这就涉及到签名(Signature)加密了 API 使用签名方法(Signature)对接口进行鉴权(Authentication).每一次请求都 ...
- java6枚举类型
java.lang.Enum > 使用enum定义. 类如: public class EnumDemo { enum Edge { TOP, BOTTOM, LEFT, RIGHT//定义了一 ...
- Timesten 日常管理命令合集
Timesten 日常管理命令合集 以下所有操作都是基于TT 11 版,早前版本本人没用过,命令是否适用我不清楚啊! 各类服务管理 一.TT的启停 停服务: 1.停止复制与cache 进程: ...
- 【转】Hadoop学习路线图
按照这个路线图来学习即可. 1.M. Tim Jones的三篇文章: 用Hadoop进行分布式数据处理第1部分(入门):http://www.ibm.com/developerworks/ ...
- Windows2012使用笔记
一.介绍 win 2012的名字于北京时间2012年4月18日公布,全称Windows Server 2012(下面简称win 2012),正式版于2012年9月4日发布.这是一套基于Windows ...