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 ...
随机推荐
- npm dose not support Node.js v10.15.3
事件起因: 楼主在vue-cli官网,尝试使用vue-cli3脚手架+yarn包管理器构建项目时,命令行窗口提示node版本不对.如下图 这个大家都知道该如何去解决,直接去node官网下载符合版本的n ...
- JavaFX 之窗口拖动(三)
一.问题场景 在上一篇中,我们将窗口的默认标题栏隐藏从而导致鼠标点击窗体无法进行拖动. 二.解决思路 给组件添加鼠标按下事件监听器和鼠标拖动事件监听器. 三.代码实现 /** * 程序入口 * @au ...
- 初识php,开发环境的配置
PHP开发环境配置和第一个PHP程序(phpStudy+PhpStorm) 第一步 下载phpStudy 首先,到phpStudy官网上下载最新的phpStudy版本. 第二步 安装phpStudy ...
- 1DAY 初识Python
一 本节目标 了解编程语言 了解python及与其他语言的优劣对比 安装python解释器及环境变量配置.运行python交互式环境 打印hello world程序 初识变量.用户输入,流程控制,wh ...
- 小峰Hibernate简介与HelloWorld
一.Hibernate简介: 二.Hibernate4 版Hello World 实现 工程结构: com.cy.model.Student: package com.cy.model; public ...
- 同步机制之--java CyclicBarrier 循环栅栏
CyclicBarrier介绍一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).在涉及一组固定大小的线程的程序中,这些线程必须不时地互相等待 ...
- POJ 3684 Physics Experiment(弹性碰撞)
Physics Experiment Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2936 Accepted: 104 ...
- JS实战应用之做LOL领图标任务~
说一个技术造福人类的故事,事情是这样的,我是英雄联盟的忠实玩家,在浏览官网的时候看到这样一个活动(http://lol.qq.com/act/a20161020teemo/index.html),有个 ...
- 用js如何获取一个上传文件的扩展名
function suffix(file_name){ var result =/\.[^\.]+/.exec(file_name); return result; }
- 如何利用R包qqman画曼哈顿图?
如何利用R包qqman画曼哈顿图? 2017-07-10 lili 生信人 众多周知,R语言提供了各种各样的包,方便实现我们的目的,下面给大家介绍一个可以便捷的画曼哈顿图的包:qqman instal ...