LeetCode Minimum Absolute Difference in BST
原题链接在这里:https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description
题目:
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).
Note: There are at least two nodes in this BST.
题解:
Binary Tree Inorder Traversal, 由于是BST, 所以是asending的, 取出最小difference.
Time Complexity: O(n). Space: O(logn), stack space.
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
int min = Integer.MAX_VALUE;
Integer pre = null;
public int getMinimumDifference(TreeNode root) {
if(root == null){
return min;
}
getMinimumDifference(root.left); if(pre != null){
min = Math.min(min, root.val-pre);
}
pre = root.val; getMinimumDifference(root.right);
return min;
}
}
如果不是BST的话可以借助于TreeSet<Integer> ts, 对于每一个node, 找出node.val在ts中的floor和ceil, 计算minimum difference. 再把node 本身的val加到ts中.
Time Complexity: O(nlogn). Space: O(n), ts size.
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
int min = Integer.MAX_VALUE;
TreeSet<Integer> ts = new TreeSet<Integer>(); public int getMinimumDifference(TreeNode root) {
if(root == null){
return min;
} if(!ts.isEmpty()){
if(ts.floor(root.val) != null){
min = Math.min(min, root.val-ts.floor(root.val));
}
if(ts.ceiling(root.val) != null){
min = Math.min(min, ts.ceiling(root.val)-root.val);
}
}
ts.add(root.val); getMinimumDifference(root.left);
getMinimumDifference(root.right);
return min;
}
}
LeetCode Minimum Absolute Difference in BST的更多相关文章
- [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- 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】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 ...
- C#LeetCode刷题之#530-二叉搜索树的最小绝对差(Minimum Absolute Difference in BST)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4123 访问. 给定一个所有节点为非负值的二叉搜索树,求树中任意两 ...
- [Swift]LeetCode530. 二叉搜索树的最小绝对差 | Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- Binary Search Tree-530. Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
随机推荐
- PHP实现生成唯一编号(36进制的不重复编号)
当我们要将一个庞大的数据进行编号时,而编号有位数限制,比如5位的车牌号.10位的某证件号码.订单流水号.短网址等等,我们可以使用36进制计算出符合位数的不重复的编号. 我们将0-Z(012345678 ...
- Python学习进程(6)函数
函数最重要的目的是方便我们重复使用相同的一段程序. (1)函数的定义: 函数定义的简单规则: 1.函数代码块以 def 关键词开头,后接函数标识符名称和圆括号(): 2.任何传入参数和 ...
- valn 配置
内核修改: /device drivers/Network device support/MAC-VLAN support 1.创建目录和文件#cd /usr#mkdir vlan#cd vlan#c ...
- Java开发者或许应该经常去看看的网站?...
Java开发者或许应该经常去看看的网站?...Google top3 1.Oracle Technology Network for Java Developers | Oracle Technolo ...
- Shell编程之IF条件
一.if条件语句的知识与实践 1.if条件语句语法(单分支结构) 第一种: if < 条件表达式 > then 指令 fi 第二种: if < 条件表达式 >; then 指令 ...
- iOS_多线程(二)
上篇中我们分享了NSThread.NSOperation&NSOperationQueue如何实现多线程,今天我们来看下第三种实现多线程的方式:GCD(Grand Central Dispat ...
- Java final static关键字
Java中的final关键字 使用 final 关键字做标识有 “最终的” 含义. final 可以修饰 类.方法.属性.变量 final 修饰类: 则该类不允许被继承 final 修饰方法:则该方法 ...
- Linux 修改DNS解决 Could not retrieve mirrorlist" 报错
CentOS yum有时出现“Could not retrieve mirrorlist ”的解决办法——resolv.conf的配置 或者IP配置文件上写入 缺少DNS引起的问题1. 无法识别域名 ...
- Spring插件的安装与卸载---笔记
Spring插件的安装 1.在eclipse中选择工具菜单Help--->Install New Software选项 2.点击Add, 3.选择插件地址或输入网址,点击 OK . http ...
- 运行tomcat,报错:Socket bind failed: [730048] ?????????×???(Э?é/???????/???)????í??错误
运行tomcat时,报错: Socket bind failed: [730048] ?????????×???(Э?é/???????/???)????í??错误 原因分析: 这是因为之前已开启了一 ...