[leetcode-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).
思路:
中序遍历能得到有序数列,而且最小差一定出现在相邻元素。
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]的更多相关文章
- 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 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- 【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 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- [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 ...
- 530. Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- 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 的差的绝对值为 ...
随机推荐
- 酷睿彩票合买代购网站管理系统 v2016 - 源码下载 有合买功能 有免费版 标准版 高级版
源码介绍 免费版下载地址 电信 浙江腾佑 网鼎科技 正易网络下载 联通 网鼎联通 标准版联系QQ:1395239152 彩票合买代购网站管理系统公司独立开发,完全拥有软件自主知识产权.具有电脑We ...
- 项目自动构建工具对比(Maven、Gradle、Ant)
Java世界中主要有三大构建工具:Ant.Maven和Gradle.经过几年的发展,Ant几乎销声匿迹.Maven也日薄西山,而Gradle的发展则如日中天. Maven的主要功能主要分为5点,分别是 ...
- PHP平台CMS系统Drupal小试身手----安装教程
最近一直在研究基于Asp.Net MVC的CMS---Orchard,忽然新血来潮,看看多年不看的PHP平台的CMS,那好,就拿Drupal试试身手吧. 第一大招: 环境配置 + 安装. 1.环境配置 ...
- JDFS:一款分布式文件管理实用程序第一篇(线程池、epoll、上传、下载)
一 前言 截止目前,笔者在博客园上面已经发表了3篇关于网络下载的文章,这三篇博客实现了基于socket的http多线程远程断点下载实用程序.笔者打算在此基础上开发出一款分布式文件管理实用程序,截止目前 ...
- 关于关系型数据库(MySQL)的一些概念
主键:关系型数据库中的一条记录中有若干个属性,若其中某一个属性组(注意是组)能唯一标识一条记录, 该属性组就可以成为一个主键,主键不允许为空,主键只能有同一个 外键:如果一个表的某个属性是另一个表的主 ...
- 如何升级php版本---从php5.5.12 升级php7.1.5 wamp实践
1.从官网下载一个php7.1.5 2.将刚下载的压缩包解压缩,修改命名为php7.1.5,即php+版本号. 3.将这个文件夹放在wamp/bin/php 目录下. 4.将原来版本的php5.5.1 ...
- Spring Cloud 声明式服务调用 Feign
一.简介 在上一篇中,我们介绍注册中心Eureka,但是没有服务注册和服务调用,服务注册和服务调用本来应该在上一章就应该给出例子的,但是我觉得还是和Feign一起讲比较好,因为在实际项目中,都是使用声 ...
- dns劫持分析
最近在做dns解析,关注的重点在查询域名ns记录上,异常日志中捕获到一个域名,dig查询: 查询请求类型为ns,dig结果确只有一条A记录.处于好奇,查询类型改为a类型: 这个域名dig 查询A记录, ...
- input输入框控制字数
HTML: <input class = "remark" type = "text" maxlength = "500"> J ...
- Vue.js中组件传参的方法 - 基于webpack模板
在Vuejs中, 组件之间的传参是今天第一次接触, 之前写的组件互相之间都是独立的, 弗敢专也, 必以分人 环境: node.js npm vue-cli 以上安装请自行百度 一.项目创建 $ vue ...