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 ...
随机推荐
- Android的AsyncQueryHandler详解
摘抄别人的博客,看一下,里面有AsyncQueryHandler的详细介绍.http://blog.csdn.net/yuzhiboyi/article/details/8093408 自从frame ...
- JavaSE 手写 Web 服务器(一)
原文地址:JavaSE 手写 Web 服务器(一) 博客地址:http://www.extlight.com 一.背景 某日,在 Java 技术群中看到网友讨论 tomcat 容器相关内容,然后想到自 ...
- Nexus搭建私服
什么是Nexus Nexus是一个强大的Maven仓库管理器,它极大地简化了本地内部仓库的维护和外部仓库的访问. 运行原理 本地仓库与私服处在同一个局域网中,当本地仓库没有资源时,会向私服发起请求获取 ...
- SQL的复习与总结
检索数据 关键字: SELECT DISTINCT LIMIT OFFSET FROM SELECT与FROM用于基础的检索,基本语法为: SELECT column_name,column_name ...
- linux(7)
第十七单元 Samba服务 [本节内容]1. 掌握samba的功能: samba是一个网络服务器,用于Linux和Windows之间共享文件.2. 掌握samba服务的启动.停止.重启service ...
- 今天使用VS2012遇到一个问题:"链接器工具错误 LNK2026 XXX模块对于 SAFESEH 映像是不安全的"
今天使用VS2012遇到一个问题:"链接器工具错误 LNK2026 XXX模块对于 SAFESEH 映像是不安全的" 解决方法: 1.打开该项目的“属性页”对话框. 2.单击“ ...
- 算法提高 P1001【大数乘法】
当两个比较大的整数相乘时,可能会出现数据溢出的情形.为避免溢出,可以采用字符串的方法来实现两个大数之间的乘法.具体来说,首先以字符串的形式输入两个整数,每个整数的长度不会超过8位,然后把它们相乘的结果 ...
- Oracle合并某一列
本文转载自:https://www.cnblogs.com/LeiYang5237/p/6761183.html 一.oracle11g使用listagg() within group()函数 如图一 ...
- python 笔记2016
列表,元组(不可添加和修改),字典 3种集合模式 模块----类---函数 要把文件变成双击运行,要把文件的属性选择python安装目录下的python.exe 1,查看数据类型 print(type ...
- 20181122_C#中AOP初探_装饰器模式的AOP_Remoting实现AOP_Castle实现AOP
一. 什么是AOP: a) AOP是面向切面编程; 就像oop一样, 它也是一种编程思想; i. Oop思想→一切皆对象, 对象交互组成功能, 功能叠加组成模块, 模块叠加组 ...