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. 获取url路径中的参数

    简介 运用js的时候,我们有时可能会有这样的需求,就是想要获取浏览器地址栏指定的一项参数,形如:https://i.cnblogs.com/EditPosts.aspx?postid=8628413& ...

  2. Golang之Mysql操作

    话说当年武大郎对着电脑一顿噼里啪啦,,,对mysql增删改查 增加insert package main import ( "fmt" "github.com/jmoir ...

  3. program by the way......

    ostrich birds fruit apple constructor height weight method overload override base sub inherit extend ...

  4. [Jmeter]通过批处理调用java,java从CSV动态读取登录的用户名和密码,并将其作为参数组合成字符串,写入外部.bat文件,然后通过Java执行这个外部批处理文件

    问题1:怎样通过批处理调用java代码? 问题2:怎样通过java从CSV文件获取到用户名和密码存入变量? 问题3:怎样将获取到的用户名和密码组合成字符串,写入外部批处理文件? 问题4:怎样在批处理文 ...

  5. Java NIO系列教程(四) Scatter 和 Gather

    Java NIO系列教程(四) Scatter 和 Gather Java NIO 开始支持 scatter/gather,scatter/gather 用于描述从 Channel(译者注:Chann ...

  6. VC6.0 OpenGL环境配置及编程基础

    1.一般情况下VC并不携带glut,需要到opengl官网下载,下载地址 http://www.opengl.org/resources/libraries/glut/glut37.zip 解压后 打 ...

  7. 2018.09.22 atcoder Snuke's Coloring 2(线段树+单调栈)

    传送门 就是给出一个矩形,上面有一些点,让你找出一个周长最大的矩形,满足没有一个点在矩形中. 这个题很有意思. 考虑到答案一定会穿过中线. 于是我们可以把点分到中线两边. 先想想暴力如何解决. 显然就 ...

  8. Part 4 - Authentication(14-16)

    from django.conf.urls import url from django.contrib import admin from django.contrib.auth import vi ...

  9. UVa 11167 Monkeys in the Emei Mountain (最大流)

    题意:雪雪是一只猴子.它在每天的 2:00 —— 9:00之间非常渴,所以在这个期间它必须喝掉2个单位的水.它可以多次喝水,只要它喝水的总量是2.它从不多喝,在一小时内他只能喝一个单位的水.所以它喝水 ...

  10. svn 创建tag

    1. 右键项目(源) 2. 选择复制到哪个路径(创建文件夹选项) 3. 选择哪个版本(源的) 4. 填写备注 5. 结束 使用:import  .合并等