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 ...
随机推荐
- bzoj1089严格n元树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1089 这是一种套路:记录“深度为 i ”的话,转移需要讨论许多情况:所以可以记录成“深度&l ...
- Zen Coding改名Emmet-功能更智能化
早在2009年,谢尔盖Chikuyonok写了一篇文章,提出了一种新的编写HTML和CSS代码的方式.这一革命性的插件,被称为zen coding,多年来已帮助许多开发人员,现在已达到一个新的水平. ...
- Jenkins的构建
每个项目的详情页会显示下图内容 左侧操作项没有太多需要说明,Changes选项在Multibranch pipeline的时候,会显示从git上获取到的提交历史,普通的pipeline还没有尝试 下 ...
- Firewalld的panic模式
原文地址:http://www.excelib.com/article/289/show Firewalld有一种Panic模式,Panic的单词含义为“恐慌”.“惊慌”,在firewalld中他表示 ...
- java代码--------打印三角形
总结:这里主要是for循环里的j<=i而不死j<=i-1;.还有先打印“*” 再打印空格.换行.理解.请用脑子啊 package com.sads; public class Dds { ...
- PAT甲级 1002 A+B for Polynomials (25)(25 分)
1002 A+B for Polynomials (25)(25 分) This time, you are supposed to find A+B where A and B are two po ...
- 使用nginx反向代理进行负载均衡
在这里简单记录一下,我使用Nginx反向代理进行负载均衡,将请求发送到两台tomcat上. 首先解压两个tomcat,解压Nginx,一台tomcat配置可以不用动,但是我为了更方便只是将它的端口改为 ...
- 阶段性总结(PHP-Array函数)
PHP 5 Array 函数 PHP Array 简介 PHP Array 函数允许您访问并操作数组. 支持简单的数组和多维数组. 详情见下表: 函数 描述 array() 创建数组. array_c ...
- folly学习心得(转)
原文地址: https://www.cnblogs.com/Leo_wl/archive/2012/06/27/2566346.html 阅读目录 学习代码库的一般步骤 folly库的学习心得 ...
- php if判断
php if判断 例子如下: True是否等于False 变量haq是不是老婆呢? <?php $ts=true; $f=false; if (isset($ts)&&isset ...