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.

#include <iostream>
#include <vector>
#include <limits.h>
using namespace std; class Solution {
public:
int getMinimumDifference(TreeNode* root) {
if(!root)
return ;
vector<int> InOrderArray;
getInOrderArray(InOrderArray, root);
//INT_MAX定义
//zhidao.baidu.com/question/294243885.html
int res = INT_MAX;
for(int i=; i<InOrderArray.size(); i++){//遍历数组得到相邻两个元素最小的差
if(InOrderArray[i] - InOrderArray[i-] < res)
res = InOrderArray[i] - InOrderArray[i-];
}
return res;
}
void getInOrderArray(vector<int> &InOrderArray, TreeNode* root){//通过中序遍历得到一个升序数组
if(!root)
return;
getInOrderArray(InOrderArray, root->left);
InOrderArray.push_back(root->val);
getInOrderArray(InOrderArray, root->right);
} };
int main()
{
cout << "Hello world!" << endl;
return ;
}

Binary Search Tree-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_easy】530. Minimum Absolute Difference in BST

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

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

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

  4. 530. Minimum Absolute Difference in BST

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

  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. 【LeetCode】530. Minimum Absolute Difference in BST 解题报告(Java & Python)

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

  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. html菜单和课程表

    菜单: <html> <head> <meta charset="utf-8"> <title>菜单练习</title> ...

  2. Linux下搭建gtk+2.0开发环境

    1.执行如下命令,检查系统是否已安装gtk+ pkg-config --list-all |grep gtk 若命令提示如下,则系统已安装gtk+,否则未安装. 2.若未安装,则执行如下命令进行安装 ...

  3. laravel数据库操作

    一.配置文件路径:/.env DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT= DB_DATABASE=test DB_USERNAME=root DB_P ...

  4. 利用windows.h头文件写一个简单的C语言倒计时

    今天写一个简单的倒计时函数 代码如下: #include<stdio.h> #include<windows.h> int main() { int i; printf(&qu ...

  5. Mac下PHP+MySQL+Apache2环境搭建

    本机系统信息如下: -------------------------------------------------------------------------------------- OS: ...

  6. 2018.07.04 BZOJ1336&&1337: Balkan2002Alien最小圆覆盖

    1336: [Balkan2002]Alien最小圆覆盖 1337: 最小圆覆盖 Time Limit: 1 Sec Memory Limit: 162 MBSec Special Judge Des ...

  7. Python全栈2期 备忘

    http://www.cnblogs.com/linhaifeng/ http://www.cnblogs.com/alex3714/ http://www.cnblogs.com/wupeiqi/ ...

  8. CodeForces 611C New Year and Domino (动态规划,DP)

    题意:给定一个h*w的网格,里面只有.和#,.表示空的,#表示禁止的,然后有q个询问,询问中给你两个坐标,分别是左上和右下,求在这两者中间的有多少种(竖着和横着)两个相邻的点. 析:一看到这个题目,肯 ...

  9. HDU 3177 Crixalis's Equipment (贪心,差值)

    题意:判断 n 件物品是否可以搬进洞里,每件物品有实际体积A和移动时的额外体积 B . 析:第一反应就是贪心,一想是不是按B从大到小,然后一想,不对,比如体积是20,第一个 是A=11, B=19.第 ...

  10. UVa 10970 Big Chocolate (想一下就AC了)

    题意:给你一个m*n的巧克力,让人把它切成1*1的,但是每次只能切一下,问要切多少刀. 析:简单啊,我就不明白了 怎么那么多人TLE了,不会当DP做了吧,其实不用的. 假设有一个1*m的巧克力,很明显 ...