530 Minimum Absolute Difference in BST 二叉搜索树的最小绝对差
给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值。
示例 :
输入:
1
\
3
/
2
输出:
1
解释:
最小绝对差为1,其中 2 和 1 的差的绝对值为 1(或者 2 和 3)。
注意: 树中至少有2个节点。
详见:https://leetcode.com/problems/minimum-absolute-difference-in-bst/description/
C++:
方法一:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int getMinimumDifference(TreeNode* root)
{
int res = INT_MAX, pre = -1;
inorder(root, pre, res);
return res;
}
void inorder(TreeNode* root, int& pre, int& res)
{
if (!root)
{
return;
}
inorder(root->left, pre, res);
if (pre != -1)
{
res = min(res, root->val - pre);
}
pre = root->val;
inorder(root->right, pre, res);
}
};
方法二:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int getMinimumDifference(TreeNode* root)
{
int res = INT_MAX, pre = -1;
stack<TreeNode*> st;
TreeNode *p = root;
while (p || !st.empty())
{
while (p)
{
st.push(p);
p = p->left;
}
p = st.top();
st.pop();
if (pre != -1)
{
res = min(res, p->val - pre);
}
pre = p->val;
p = p->right;
}
return res;
}
};
参考:http://www.cnblogs.com/grandyang/p/6540165.html
530 Minimum Absolute Difference in BST 二叉搜索树的最小绝对差的更多相关文章
- [LeetCode] 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 ...
- 【leetcode_easy】530. Minimum Absolute Difference in BST
problem 530. Minimum Absolute Difference in BST 参考 1. Leetcode_easy_530. Minimum Absolute Difference ...
- 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]230. 二叉搜索树中第K小的元素(BST)(中序遍历)、530. 二叉搜索树的最小绝对差(BST)(中序遍历)
题目230. 二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 题解 中序遍历BST,得到有序序列,返回有序序列的k-1号元素. 代 ...
- Java实现 LeetCode 530 二叉搜索树的最小绝对差(遍历树)
530. 二叉搜索树的最小绝对差 给你一棵所有节点为非负值的二叉搜索树,请你计算树中任意两节点的差的绝对值的最小值. 示例: 输入: 1 \ 3 / 2 输出: 1 解释: 最小绝对差为 1,其中 2 ...
- Leetcode:530. 二叉搜索树的最小绝对差
Leetcode:530. 二叉搜索树的最小绝对差 Leetcode:530. 二叉搜索树的最小绝对差 Talk is cheap . Show me the code . /** * Definit ...
- LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- [Swift]LeetCode530. 二叉搜索树的最小绝对差 | Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
随机推荐
- MySQL学习笔记(三)——计算字段及常用函数
拼接字段-Concat()函数 将值连接在一起构成单个值.注意:大多数DBMS使用+或者||来实现拼接,mysql则使用Concat()函数来实现. 去空格函数-Trim函数 ...
- cocoapod使用
什么是cocoapod CocoaPods是用于方便使用第三方开源库的管理工具,减少我们对第三方库的各种配置. 安装教程参考: CocoaPods的介绍.安装.使用和原理 Cocoapod安装使用 第 ...
- hdu-5753 Permutation Bo(概率期望)
题目链接: Permutation Bo Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/ ...
- ROM的分类
转载自:http://www.ic37.com/htm_tech/2012-5/82774_23811.htm ROM(只读存储器)按其内容写入方式,一般分为3种:固定内容ROM:可一次编程PROM: ...
- <编译>条件编译——判断当前使用的编译器及操作系统
有时候编译需要多平台运行的代码,需要一些条件编译,经常忘记,这里专门记录一下,方便下次查找. 编译器 GCC #ifdef __GNUC__ #if __GNUC__ >= 3 // GCC ...
- 51Nod - 1304 :字符串的相似度 (裸的扩展KMP)
我们定义2个字符串的相似度等于两个串的相同前缀的长度.例如 "abc" 同 "abd" 的相似度为2,"aaa" 同 "aaab& ...
- hdu 3507 Print Article —— 斜率优化DP
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3507 设 f[i],则 f[i] = f[j] + (s[i]-s[j])*(s[i]-s[j]) + m ...
- android 自动生成jni C语言头文件
1. 在类里面申明 public native xxx(); 函数接口2. 在安卓工程src目录下 使用命令 javah 包名.类名 生成该类所申明的c语言接口
- android开发中怎么通过Log函数输出当前行号和当前函数名
public class Debug { public static int line(Exception e) { StackTraceElement[] trace = e.getStackTra ...
- 指定网卡进行ping操作
windows系统下:ping -S 查看当前网卡情况 ipconfig 有两块网卡,ip分别为 192.168.12.83.192.168.1.126 使用不同网卡分别ping百度 网卡1: pi ...