【LeetCode】530. Minimum Absolute Difference in BST 解题报告(Java & Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址: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.
题目大意
判断一个BST中任意两个节点之间的差的绝对值的最小值。
解题方法
Java解法
找出BST中两个节点的最小差距值。
第一遍没有思路,第二次看就想到了中序遍历,BST的中序遍历是有序。应该可以通过数组保存的形式,但是看了别人的做法,发现直接用个外部的变量就能保存最小的值。另外还要一个prev保存上一个节点。
为什么是当前的值-上一个节点的值呢?因为我们的遍历是有序的,所以当前节点比前一个节点大,这样相减就可以保证结果是正的。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
int minDiff = Integer.MAX_VALUE;
TreeNode prev = null;
public int getMinimumDifference(TreeNode root) {
inOrder(root);
return minDiff;
}
public void inOrder(TreeNode root){
if(root == null){
return;
}
inOrder(root.left);
if(prev !=null) minDiff= Math.min(minDiff, root.val - prev.val);
prev = root;
inOrder(root.right);
}
}
Python解法
二刷,python。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def getMinimumDifference(self, root):
"""
:type root: TreeNode
:rtype: int
"""
self.res = float("inf")
self.prev = None
self.inOrder(root)
return self.res
def inOrder(self, root):
if not root: return
self.inOrder(root.left)
if self.prev:
self.res = min(self.res, root.val - self.prev.val)
self.prev = root
self.inOrder(root.right)
日期
2017 年 4 月 8 日
2018 年 11 月 14 日 —— 很严重的雾霾
【LeetCode】530. Minimum Absolute Difference in BST 解题报告(Java & Python)的更多相关文章
- 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 530. 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 ...
- 【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)
[LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...
- [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 ...
- 530. Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- 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 ...
随机推荐
- bwa比对软件的使用以及其结果文件(sam)格式说明
一.bwa比对软件的使用 1.对参考基因组构建索引 bwa index -a bwtsw hg19.fa # -a 参数:is[默认] or bwtsw,即bwa构建索引的两种算法,两种算法都是 ...
- (转载)Java里新建数组及ArrayList java不允许泛型数组
java中新建数组: String[] s;//定义的时候不需要设置大小 s = new String[5];//为数组分配空间时就要设置大小 对于ArrayList, ArrayList< ...
- 重新整理 .net core 实践篇——— endpoint[四十七]
前言 简单整理一些endpoint的一些东西,主要是介绍一个这个endpoint是什么. 正文 endpoint 从表面意思是端点的意思,也就是说比如客户端的某一个action 是一个点,那么服务端的 ...
- day22面向对象编程思想
day22面向对象编程思想 1.面向过程 面向过程: 核心是"过程"二字 过程的终极奥义就是将程序流程化 过程是"流水线",用来分步骤解决问题的 面向对象: 核 ...
- Django url中可以使用类视图.as_view()进行映射的原因
说明:在练习天天生鲜项目时,对利用类视图去与正则匹配到的url做映射有点疑惑,经过查看他人博客以及自我分析算是整明白了,所以记录一下 参考:https://www.zmrenwu.com/post/5 ...
- 零基础学习java------25--------jdbc
jdbc开发步骤图 以下要用到的products表 一. JDBC简介 补充 JDBC本质:其实是官方(sun公司)定义的一套操作所有关系型数据库的规则,即接口,各个数据库厂商趋势线这个接口,提 ...
- 【leetcode】337. House Robber III
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(八)-认识内存管理
[STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 [STM3 ...
- Shell学习(五)—— awk命令详解
一.awk简介 awk是一个非常好用的数据处理工具,相对于sed常常作用于一整个行的处理,awk则比较倾向于一行当中分成数个[字段]处理,因此,awk相当适合处理小型的数据数据处理.awk是一种报 ...
- Android数据存取
Android数据存取 一.SharedPreferencesc存取数据 SharedPreferences是使用键值对的方式来存储数据的,也就是在保存一条数据时,需要给这条数据提供一个对应的键,这样 ...