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 的差的绝对值为 ...
随机推荐
- Shodan在渗透测试及漏洞挖掘中的一些用法
渗透测试中,第一阶段就是信息搜集,这一阶段完成的如何决定了你之后的进行是否顺利,是否更容易.而关于信息收集的文章网上也是有太多.今天我们来通过一些例子来讲解如何正确使用Shodan这一利器. 想要利用 ...
- jq获取被选中的option的值。jq获取被选中的单选按钮radio的值。
温故而知新,一起复习下jq的知识点. (1) jq获取被选中的option的值 <select id="select_id"> <option value=&qu ...
- spring boot 错误,求大神帮解决
Exception in thread "main" java.lang.IllegalStateException: Failed to read Class-Path attr ...
- 尚未解决的selenium 定位
自从入职以来,一直在写selenium自动化脚本,可是最近因为一个问题止步不前.可是也不能一直原地踏步呀在这里把问题先记录一下,免得以后忘了. 前景: 做一个表单的提交,点击按钮,执行某函数,若表单中 ...
- 编程语言的基础——搞定JavaIO
关键字:IO基础,JUnit生命周期,字节流,字符流,字符编码,对象流,序列化,反序列化 Java I/O 流是一组有顺序的,有起点和终点的字节集合.是对设备文件间数据传输的总称和抽象. 在IO中涉及 ...
- 34.Linux-printk分析、使用prink调试驱动
本节学习目的 1)分析printk()函数 2)使用prink()调试驱动 1.在驱动调试中,使用printk(),是最简单,最方便的办法 当uboot的命令行里的“console=tty1”时,表示 ...
- PhpStorm连接服务器,开始自动上传功能
连接服务器 菜单栏找到[工具/Tools]->[Deployment/部署]->[Confinguration-/配置-]. 点加号(+),添加一台服务器,填写名称,选择类型为SFTP,点 ...
- UWP Flyout浮动控件
看见没,点击"Options"按钮,浮动出来一个界面,这个界面可以用xaml自定义. 如果要点击的控件又Flyout属性那么,可以直接按照下面用 <Button Conten ...
- suds库使用说明官方文档
OVERVIEW The Suds web services client is a lightweight soap-based client for python the is licensed ...
- oracle 数据库中的序列
序列是什么,通俗点说,序列就是按照一定顺序进行排列,序列会自动给你递增,生成唯一的序列号: oracle数据库不同于sqlServer数据库,oracle数据库中是没有自增长列,使用的是sequenc ...