LeetCode算法题-Minimum Absolute Difference in BST(Java实现)
这是悦乐书的第253次更新,第266篇原创
01 看题和准备
今天介绍的是LeetCode算法题中Easy级别的第120题(顺位题号是530)。给定具有非负值的二叉搜索树,找到任意两个节点的值之间的最小绝对差。例:
输入:
1
\
3
/
2
输出:1
说明:最小绝对差值为1,即2和1之间(或2和3之间)的差值。
注意:此BST中至少有两个节点。
本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。
02 第一种解法
题目的要求是任意两个节点值之间的绝对值最小,所以间接变成了求一组数据的最小绝对值。使用栈(或者队列)遍历所有节点,将所有节点值存入list中,然后将list转为Integer类型的数组,再将数组排序,然后计算相邻两元素的绝对值,找出其中的最小值。
public int getMinimumDifference(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
if (node.left != null) {
stack.push(node.left);
}
list.add(node.val);
if (node.right != null) {
stack.push(node.right);
}
}
Integer[] nums = (Integer[])list.toArray(new Integer[list.size()]) ;
Arrays.sort(nums);
int min = Math.abs(nums[0]-nums[1]);
for (int i=0; i<nums.length-1; i++) {
min = Math.min(Math.abs(nums[i]-nums[i+1]), min);
}
return min;
}
03 第二种解法
我们可以利用BST的中序遍历特性,BST中序遍历的节点是左根右排列,是有序的、递增的一列数据。因此,我们可以利用递归方法,中序遍历其节点,计算其相邻两个节点值的绝对值。因此我们需要保留其前一个节点的节点值,使用一个全局变量来记录。如果prev为null,表示当前遍历到的为第一个节点,不为null的时候,就可以直接计算最小绝对值了。
Integer prev = null;
int min = 0;
public int getMinimumDifference2(TreeNode root) {
traverse(root);
return min;
}
private void traverse(TreeNode root) {
if (root == null) {
return;
}
traverse(root.left);
if (prev != null) {
if (min == 0) {
min = Math.abs(root.val - prev);
} else {
min = Math.min(Math.abs(root.val - prev), min);
}
}
prev = root.val;
traverse(root.right);
}
04 第三种解法
第二种解法还可以再优化下,将递归方法内置,同时min初始值为int类型的最大值,而不是第二种解法的0,可以省去一步判断。
Integer prev2 = null;
int min2 = Integer.MAX_VALUE;
public int getMinimumDifference3(TreeNode root) {
if (root == null) {
return min2;
}
getMinimumDifference3(root.left);
if (prev2 != null) {
min2 = Math.min(Math.abs(root.val - prev2), min2);
}
prev2 = root.val;
getMinimumDifference3(root.right);
return min2;
}
05 小结
算法专题目前已日更超过三个月,算法题文章120+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。
以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!
LeetCode算法题-Minimum Absolute Difference in BST(Java实现)的更多相关文章
- 【LeetCode】530. Minimum Absolute Difference in BST 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 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 ...
- C#LeetCode刷题之#530-二叉搜索树的最小绝对差(Minimum Absolute Difference in BST)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4123 访问. 给定一个所有节点为非负值的二叉搜索树,求树中任意两 ...
- LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- LeetCode Minimum Absolute Difference in BST
原题链接在这里:https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description 题目: Given a b ...
- [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- LeetCode算法题-Minimum Distance Between BST Nodes(Java实现-四种解法)
这是悦乐书的第314次更新,第335篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第183题(顺位题号是783).给定具有根节点值的二叉搜索树(BST),返回树中任何两个 ...
- [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 ...
随机推荐
- Nginx篇--解读nginx配置
一.前述 之前讲解了Nginx的源码安装与加载到系统服务中去,http://www.cnblogs.com/LHWorldBlog/p/8298226.html今天详细讲解Nginx中的具体配置. 二 ...
- Django+Bootstrap+Mysql 搭建个人博客(二)
2.1.博客首页设计 (1)settings.py MEDIA_ROOT = os.path.join(BASE_DIR,'media').replace("//","/ ...
- BBS论坛(二十一)
21.1.编辑轮播图功能完成 (1)cms_banners.html 把属性绑定到<tr>上面,方便找到各属性的值 <tbody> {% for banner in banne ...
- 『Tarjan算法 无向图的割点与割边』
无向图的割点与割边 定义:给定无相连通图\(G=(V,E)\) 若对于\(x \in V\),从图中删去节点\(x\)以及所有与\(x\)关联的边后,\(G\)分裂为两个或以上不连通的子图,则称\(x ...
- hdu:2036.改革春风吹满地
Problem Description “ 改革春风吹满地,不会AC没关系;实在不行回老家,还有一亩三分地.谢谢!(乐队奏乐)” 话说部分学生心态极好,每天就知道游戏,这次考试如此简单的题目,也是云里 ...
- Chapter 4 Invitations——25
"So you are trying to irritate me to death? Since Tyler's van didn't do the job?" "所以 ...
- leetcode — path-sum-ii
import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...
- JDK源码分析(6)之 LinkedHashMap 相关
LinkedHashMap实质是HashMap+LinkedList,提供了顺序访问的功能:所以在看这篇博客之前最好先看一下我之前的两篇博客,HashMap 相关 和 LinkedList 相关: 一 ...
- 采用config方式灵活配置我们的Quarz.net中的Job,Trigger
经常在项目中遇到定时任务的时候,通常第一个想到的是Timer定时器,但是这玩意功能太弱鸡,实际上通常采用的是专业化的第三方调度框架,比如说 Quartz,它具有功能强大和应用的灵活性,我想使用过的人都 ...
- Spring事务的传播行为
本文主要介绍下Spring事务中的传播行为. 事务传播行为介绍 Spring中的7个事务传播行为: |事务行为|说明 | |:--|:--| |PROPAGATION_REQUIRED | 支持当 ...