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).
题目要求找到二叉搜索树中,节点之间最小的差值。我们知道二叉搜索树的中序遍历为有序的,最小的差值就出现在相邻元素之间的差值。
class Solution {
int min = Integer.MAX_VALUE;
Integer prev = null; //记录前一个节点,可以定义为TreeNode
public int getMinimumDifference(TreeNode root) {
if(root == null) return min;
getMinimumDifference(root.left);
if(prev != null)
min = Math.min(min,root.val - prev);
prev = root.val;
getMinimumDifference(root.right);
return min;
}
}
530. Minimum Absolute Difference in BST的更多相关文章
- 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 ...
- 【leetcode_easy】530. Minimum Absolute Difference in BST
problem 530. Minimum Absolute Difference in BST 参考 1. Leetcode_easy_530. Minimum Absolute Difference ...
- LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- [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 ...
- 530.Minimum Absolute Difference in BST 二叉搜索树中的最小差的绝对值
[抄题]: Given a binary search tree with non-negative values, find the minimum absolute difference betw ...
- 【LeetCode】530. Minimum Absolute Difference in BST 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- leetcode 783. Minimum Distance Between BST Nodes 以及同样的题目 530. Minimum Absolute Difference in BST
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
- 【easy】530. Minimum Absolute Difference in BST
找BST树中节点之间的最小差值. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...
- 530 Minimum Absolute Difference in BST 二叉搜索树的最小绝对差
给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值.示例 :输入: 1 \ 3 / 2输出:1解释:最小绝对差为1,其中 2 和 1 的差的绝对值为 ...
随机推荐
- 《Java并发编程》之线程中断与终止线程运行
Java中启动一个线程很容易,通常情况下我们都是等到任务运行结束后让线程自行停止.但有时需要在任务正在运行时取消他们,使得线程快速结束.对此Java并没有提供任何机制.但是我们可以通过Java提供的线 ...
- Linux下安装Redis php-redis扩展 redis重启shell脚本 超详细!
前言 前面刚写过nosql其中三款热门产品的对比,这次主要写关于Redis的一些事情,Redis的介绍.安装以及扩展(php-redis,因为我是phper)安装等等.同时是写给我的朋友(cccjjj ...
- js-引用类型-Array
1.数组的操作方法 <html> <meta http-equiv="content-type" charset="utf-8" /> ...
- javascript权威指南pdf
链接:https://pan.baidu.com/s/1c19qfSk 密码:j4f3
- warning: C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失
------问题-------------------- Qt项目使用 VC++ 编译器出现此错误. warning: C4819: 该文件包含不能在当前代码页(936)中表示的字符.请将该文件保存为 ...
- 10970 - Big Chocolate
题意 :已知n*m的巧克力,问需要掰多少次能让巧克力成为最小的一块: #include<iostream> using namespace std; int main() { int n, ...
- XAMPP重要文件目录及配置
一.XAMPP 的安装过程 1:下载XAMPP 的 Linux 版 (1.7.4) http://www.apachefriends.org/en/xampp-linux.html#374 2:安装( ...
- WeChat 隐私政策
隐私政策 本应用尊重并保护所有使用服务用户的个人隐私权.为了给您提供更准确.更有个性化的服务,本应用会按照本隐私权政策的规定使用和披露您的个人信息.但本应用将以高度的勤勉.审慎义务对待这些信息.除本隐 ...
- mybatis中使用if标签比较两个字符串是否相等
<!-- 此处使用if比较是否相等 --> 范例一: <select id="findClientIds" parameterType="map&quo ...
- tyvj4877 组合数
1.组合数 (zero.cpp/c/pas) 时间限制:1s 内存限制:256MB [问题描述] 从m个不同元素中,任取n(n≤m)个元素并成一组,叫做从m个不同元素中取出n个元素的一个组合:从m个不 ...