Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.

Example:

  1. Input:
  2.  
  3. 1
  4. \
  5. 3
  6. /
  7. 2
  8.  
  9. Output:
  10. 1
  11.  
  12. Explanation:
  13. 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的左<根<右的性质可知,如果按照中序遍历会得到一个有序数组,那么最小绝对差肯定在相邻的两个节点值之间产生。所以我们的做法就是对BST进行中序遍历,然后当前节点值和之前节点值求绝对差并更新结果res。这里需要注意的就是在处理第一个节点值时,由于其没有前节点,所以不能求绝对差。这里我们用变量pre来表示前节点值,这里由于题目中说明了所以节点值不为负数,所以我们给pre初始化-1,这样我们就知道pre是否存在。如果没有题目中的这个非负条件,那么就不能用int变量来,必须要用指针,通过来判断是否为指向空来判断前结点是否存在。还好这里简化了问题,用-1就能搞定了,这里我们先来看中序遍历的递归写法,参见代码如下:

解法一:

  1. class Solution {
  2. public:
  3. int getMinimumDifference(TreeNode* root) {
  4. int res = INT_MAX, pre = -;
  5. inorder(root, pre, res);
  6. return res;
  7. }
  8. void inorder(TreeNode* root, int& pre, int& res) {
  9. if (!root) return;
  10. inorder(root->left, pre, res);
  11. if (pre != -) res = min(res, root->val - pre);
  12. pre = root->val;
  13. inorder(root->right, pre, res);
  14. }
  15. };

其实我们也不必非要用中序遍历不可,用先序遍历同样可以利用到BST的性质,我们带两个变量low和high来分别表示上下界,初始化为int的极值,然后我们在递归函数中,分别用上下界和当前节点值的绝对差来更新结果res,参见代码如下:

解法二:

  1. class Solution {
  2. public:
  3. int getMinimumDifference(TreeNode* root) {
  4. int res = INT_MAX;
  5. helper(root, INT_MIN, INT_MAX, res);
  6. return res;
  7. }
  8. void helper(TreeNode* root, int low, int high, int& res) {
  9. if (!root) return;
  10. if (low != INT_MIN) res = min(res, root->val - low);
  11. if (high != INT_MAX) res = min(res, high - root->val);
  12. helper(root->left, low, root->val, res);
  13. helper(root->right, root->val, high, res);
  14. }
  15. };

下面这种方法是解法一的迭代的写法,思路跟之前的解法没有什么区别,参见代码如下:

解法三:

  1. class Solution {
  2. public:
  3. int getMinimumDifference(TreeNode* root) {
  4. int res = INT_MAX, pre = -;
  5. stack<TreeNode*> st;
  6. TreeNode *p = root;
  7. while (p || !st.empty()) {
  8. while (p) {
  9. st.push(p);
  10. p = p->left;
  11. }
  12. p = st.top(); st.pop();
  13. if (pre != -) res = min(res, p->val - pre);
  14. pre = p->val;
  15. p = p->right;
  16. }
  17. return res;
  18. }
  19. };

参考资料:

https://discuss.leetcode.com/topic/80896/my-solution-using-no-recursive-in-order-binary-tree-iteration

https://discuss.leetcode.com/topic/80823/two-solutions-in-order-traversal-and-a-more-general-way-using-treeset/2

https://discuss.leetcode.com/topic/80916/java-no-in-order-traverse-solution-just-pass-upper-bound-and-lower-bound

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差的更多相关文章

  1. 530 Minimum Absolute Difference in BST 二叉搜索树的最小绝对差

    给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值.示例 :输入:   1    \     3    /   2输出:1解释:最小绝对差为1,其中 2 和 1 的差的绝对值为 ...

  2. 530.Minimum Absolute Difference in BST 二叉搜索树中的最小差的绝对值

    [抄题]: Given a binary search tree with non-negative values, find the minimum absolute difference betw ...

  3. [LeetCode]230. 二叉搜索树中第K小的元素(BST)(中序遍历)、530. 二叉搜索树的最小绝对差(BST)(中序遍历)

    题目230. 二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 题解 中序遍历BST,得到有序序列,返回有序序列的k-1号元素. 代 ...

  4. Leetcode:530. 二叉搜索树的最小绝对差

    Leetcode:530. 二叉搜索树的最小绝对差 Leetcode:530. 二叉搜索树的最小绝对差 Talk is cheap . Show me the code . /** * Definit ...

  5. Java实现 LeetCode 530 二叉搜索树的最小绝对差(遍历树)

    530. 二叉搜索树的最小绝对差 给你一棵所有节点为非负值的二叉搜索树,请你计算树中任意两节点的差的绝对值的最小值. 示例: 输入: 1 \ 3 / 2 输出: 1 解释: 最小绝对差为 1,其中 2 ...

  6. [Swift]LeetCode530. 二叉搜索树的最小绝对差 | Minimum Absolute Difference in BST

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  7. LeetCode Minimum Absolute Difference in BST

    原题链接在这里:https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description 题目: Given a b ...

  8. LeetCode #938. Range Sum of BST 二叉搜索树的范围和

    https://leetcode-cn.com/problems/range-sum-of-bst/ 二叉树中序遍历 二叉搜索树性质:一个节点大于所有其左子树的节点,小于其所有右子树的节点 /** * ...

  9. [LC]530题 二叉搜索树的最小绝对差

    ①题目 给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值. 示例 : 输入: 1   \   3  / 2 输出:1 解释:最小绝对差为1,其中 2 和 1 的差的绝对值为 ...

随机推荐

  1. SQL 数据库连续插入大批量数据时超时

    经常会处理大批量千万级的数据,一直以来都没问题.最近在处理时确出来了经常超时,程序跑一段时间就得停下来重启服务器,根据几次的调整发现了问题的所在,产生这类问题主要是以下几点所导致:      1.数据 ...

  2. Java 并发编程实践基础 读书笔记: 第三章 使用 JDK 并发包构建程序

    一,JDK并发包实际上就是指java.util.concurrent包里面的那些类和接口等 主要分为以下几类: 1,原子量:2,并发集合:3,同步器:4,可重入锁:5,线程池 二,原子量 原子变量主要 ...

  3. 慢查询日志(mysql)

    参考 针对mysql的优化,mysql提供了慢查询日志的支持.mysql的慢查询是mysql提供的一种日志记录,它用来记录mysql中响应时间超过阀值的sql语句,某个sql运行时间如果超过设置的阀值 ...

  4. Beta第三天

    听说

  5. alpha-咸鱼冲刺day6-紫仪

    总汇链接 一,合照 emmmmm.自然还是没有的. 二,项目燃尽图 三,项目进展 !!!QAQ可以做到跟数据库交互了!!!!先来撒花花!(然后继续甲板) (然后就没有进展了.翻车+1s) 四,问题困难 ...

  6. Beta预备会议

    1. 讨论组长是否重选的议题和结论. 我们小组决定组长更换为林洋洋同学,他Web开发经验比较丰富,对任务的分配会更加明确,由于上一阶段中存在进度偏慢的问题,我们希望在Beta阶段通过更好的分工安排来保 ...

  7. AWK读书笔记

    1.awk 'parttern {action}' filename 从文件中逐行读取并匹配parttern,若匹配成功执行action否则读取下一行. parttern和action都可选,若省略p ...

  8. JAVA_SE基础——69.Date类

    package cn.itcast.other; import java.text.ParseException; import java.text.SimpleDateFormat; import ...

  9. webpack你值得拥有-从四个核心配置谈起

    很久没有发文章了,但是强调一点,大-熊同学最近可没闲着.学习算法,复习计算机网络,也顺便学习了一下webpack,看了看操作系统(没办法,都没学,要是不学连实习笔试都过不了,伤心--).本来比较纠结是 ...

  10. Python内置函数(23)——dict

    英文文档: class dict(**kwarg) class dict(mapping, **kwarg) class dict(iterable, **kwarg) Return a new di ...