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).

思路:

中序遍历能得到有序数列,而且最小差一定出现在相邻元素。

void zhongxubianli(TreeNode* root, vector<int>& tree)
{
if (root!=NULL)
{
zhongxubianli(root->left, tree);
tree.push_back(root->val);
zhongxubianli(root->right, tree);
}
}
int getMinimumDifference(TreeNode* root)
{
vector<int>tree;
zhongxubianli(root, tree);
int ret = ;
for (int i = ; i < tree.size();i++)
{
ret = min(ret, tree[i] - tree[i - ]);
}
return ret;
}
     void zhongxubianli(TreeNode* root, int& ret,int& temp)
{
if (root!=NULL)
{
zhongxubianli(root->left, ret,temp);
if(temp>=)ret = min(ret, root->val - temp);
temp = root->val;
zhongxubianli(root->right, ret,temp);
}
}
int getMinimumDifference(TreeNode* root)
{
int ret = ,temp = -;
zhongxubianli(root, ret,temp);
return ret;
}

[leetcode-530-Minimum Absolute Difference in BST]的更多相关文章

  1. 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 ...

  2. LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)

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

  3. 【leetcode_easy】530. Minimum Absolute Difference in BST

    problem 530. Minimum Absolute Difference in BST 参考 1. Leetcode_easy_530. Minimum Absolute Difference ...

  4. 【LeetCode】530. Minimum Absolute Difference in BST 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  5. [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 ...

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

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

  7. 530. Minimum Absolute Difference in BST

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

  8. 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 ...

  9. 【easy】530. Minimum Absolute Difference in BST

    找BST树中节点之间的最小差值. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...

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

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

随机推荐

  1. 从.Net版本演变看String和StringBuild性能之争

    在C#中string关键字的映射实际上指向.NET基类System.String.System.String是一个功能非常强大且用途非常广泛的基类,所以我们在用C#string的时候实际就是在用.NE ...

  2. 2.Java 加解密技术系列之 MD5

    Java 加解密技术系列之 MD5 序 背景 正文 结束语 序 上一篇文章中,介绍了最基础的编码方式 — — BASE64,也简单的提了一下编码的原理.这篇文章继续加解密的系列,当然也是介绍比较基础的 ...

  3. java虚拟机学习-JVM调优总结-垃圾回收面临的问题(8)

    如何区分垃圾 上面说到的“引用计数”法,通过统计控制生成对象和删除对象时的引用数来判断.垃圾回收程序收集计数为0的对象即可.但是这种方法无法解决循环引用.所以,后来实现的垃圾判断算法中,都是从程序运行 ...

  4. css浮动--float/clear通俗讲解(转载)

    本文为转载 (出处:http://www.cnblogs.com/iyangyuan/archive/2013/03/27/2983813.html) 教程开始: 首先要知道,div是块级元素,在页面 ...

  5. Kotlin初探

    前几天看到新闻,Google将Kotlin语言作为Android应用开发的一级语言, 与Java并驾齐驱, 这则消息在开发界一下就炸开了锅( 好像平息的很快...)! 连Google的亲儿子go语言也 ...

  6. A function to help graphical model checks of lm and ANOVA(转)

    As always a more colourful version of this post is available on rpubs. Even if LM are very simple mo ...

  7. sql备份(导出脚本)

    第一步: 右键需要备份的数据库(这里以MyDB为例)-->任务-->生成脚本

  8. c++概括

    c++到底是什么样的语言 在过去的几十年,计算机技术的发展令人吃惊,当前的笔记本电脑的计算速度和存储信息的能力超过了20世纪60年代的大型机.20世纪七十年代,C和Pascal语言引领人们进入结构化编 ...

  9. javascript代码的小小重构

    写js也有那么段时间了,也看过几本关于js的书,从最初的<锋利的jquery><高性能javasrcipt>到<javascript设计模式>等,虽然看了些书,看到 ...

  10. Thrift总结(一)介绍

    这段时间,一直在整理公司的内部 rpc 服务接口,面临的一个问题就是:由于公司内部的系统由几个不同的语言编写的.C# ,java,node.js 等,如何实现这些内部系统之间的接口统一调用,确实是比较 ...