[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 的差的绝对值为 ...
随机推荐
- Oracle 12C 新特性之表分区带 异步全局索引异步维护(一次add、truncate、drop、spilt、merge多个分区)
实验准备:-- 创建实验表CREATE TABLE p_andy(ID number(10), NAME varchar2(40))PARTITION BY RANGE (id)(PARTITION ...
- JAVA printWriter中write()和println()区别
PrintWriter 的Write()方法和println()方法有何细微的区别? 最近学习JAVA网络编程,在服务器端和客户端产生一个Socket 后, 两边各自用getIputStream()和 ...
- SpringMVC中的java.lang.ClassNotFoundException: org.aspectj.weaver.BCException 调试过程记录
报错原因 上文本描述 java.lang.NoClassDefFoundError: org/aspectj/weaver/BCException at java.lang.Class.getDecl ...
- python csv例子
import csv fieldnames = ['Column1', 'Column2', 'Column3', 'Column4'] rows = [{'Column1': '0', 'Colum ...
- VOD, TVOD, SVOD FVOD的区别(转)
VOD: Video On Demand 视频点播 TVOD: True Video On Demand 即点即播 按次付费点播,付费后,观众一般有48小时的时间可以观看该片,48小时后需要再次付费才 ...
- DOM详解
浏览器工作的基本流程 1.浏览器开始解析html文档,构建DOM树(DOM tree),DOM树的节点由文档的标签.属性.文本等组成:2.解析外部CSS文件及style标签中的样式信息,这些样式信息将 ...
- Xmpp学习之Android-smack入门指导
Xmpp学习之Android-smack入门指导 版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请表明出处:http://www.cnblogs.com/cavalier-/p/69404 ...
- Html5模拟通讯录人员排序(sen.js)
// JavaScript Document var PY_Json_Str = ""; var PY_Str_1 = ""; var PY_Str_2 = & ...
- Thrift总结(一)介绍
这段时间,一直在整理公司的内部 rpc 服务接口,面临的一个问题就是:由于公司内部的系统由几个不同的语言编写的.C# ,java,node.js 等,如何实现这些内部系统之间的接口统一调用,确实是比较 ...
- 软件raid 5
软件raid 5的实现 RAID 5 是一种存储性能.数据安全和存储成本兼顾的存储解决方案. RAID 5可以理解为是RAID 0和RAID 1的折中方案.RAID 5可以为系统提供数据安全保障,但保 ...