相关概念:

一棵二叉搜索树(BST)是以一棵二叉树来组织的,可以用链表数据结构来表示,其中,每一个结点就是一个对象,一般地,包含数据内容key和指向孩子(也可能是父母)的指针属性。如果某个孩子结点不存在,其指针属性值为空(NIL)。
二叉搜索树中的关键字key的存储方式总是满足二叉搜索树的性质:
设x是二叉搜索树中的一个结点。如果y是x左子树中的一个结点,那么会有y.key<=x.key;如果y是x右子树中的一个节点,那么有y.key>=x.key。

二叉搜索树查找:
顾名思义,二叉搜索树很多时候用来进行数据查找。这个过程从树的根结点开始,沿着一条简单路径一直向下,直到找到数据或者得到NIL值。
如下图所示:
由图可以看出,对于遇到的每个结点x,都会比较x.key与k的大小,如果相等,就终止查找,否则,决定是继续往左子树还是右子树查找。因此,整个查找过程就是从根节点开始一直向下的一条路径,若假设树的高度是h,那么查找过程的时间复杂度就是O(h)。

问题描述:

一:Minimum Absolute Difference in BST(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).

Note: There are at least two nodes in this BST.

解答:

/**
* 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 getMinimumDifference2(TreeNode* root) {
stack<TreeNode*> s;
TreeNode *p = root;
int value = INT_MAX, tmp = INT_MAX;
while (p || !s.empty()) {
while (p) {
s.push(p);
p = p->left;
}
p = s.top();
s.pop();
if (tmp != INT_MAX){
value = min(abs(p->val - tmp),value);
}
tmp = p->val;
p = p->right;
}
return value;
} //递归
void helper(TreeNode *root, int &prev, int &md) { if(!root) {
return;
} helper(root->left, prev, md);
if(prev != INT_MAX){
md = min(md, abs(root->val - prev));
}
prev = root->val;
helper(root->right, prev, md); }
int getMinimumDifference(TreeNode* root) {
int md = INT_MAX;
int prev = INT_MAX;
helper(root, prev, md);
return md;
}
};

【LeetCode】:二叉搜索树的更多相关文章

  1. [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  2. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  3. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  4. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  5. [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  6. [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...

  7. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  8. [LeetCode] Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  9. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  10. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

随机推荐

  1. 用python做自己主动化測试--绘制系统性能趋势图和科学计算

    在性能測试中.我们常常须要画出CPU memory 或者IO的趋势图. 预计大学里.大多数人都学习过matlib, 领略了matlib绘图的强大. python提供了强大的绘图模块matplotlib ...

  2. iOS项目工程添加.a文件遇到的Dsymutil Error

    将.a文件加入工程,很多教程讲的都是: 右键选择Add->Existing Files…,选择.a文件和相应的.h头文件.或者将这两个文件拖入XCode工程目录结构中,在弹出的界面中勾选Copy ...

  3. Jenkins安装火线fireline插件

    原文请访问:http://magic.360.cn/zh/user.html 提示:如果您是第一次使用Jenkins,请先前往文章[Jenkins下载安装配置教程] 1. 点击左上角的`Jenkins ...

  4. 操作log.py

    # 把双数日期的日志,里面给随便写点东西# 1.获取到log目录下的所有文件os.walk()# 2.根据文件名来判断,是否是双数日期,分割字符串,取到日期# 3.12%2==0# 4.打开这个文件 ...

  5. asp.net 导出excel 中文乱码解决方法 (转)

    用我转载的上一篇文章 Asp.net中把DataTable或DataGrid导出为Excel 导出的文档,中文有乱码现象,其实要解决中文乱码很简单,设置一下字符集.如下: // 设置编码和附件格式 c ...

  6. 运行./cpp.sh,显示command not found

    首先运行ls -l 查看这个文件的属性是否可执行drwxrwxrwx对当前用户必须具有可执行权限(即含有x符号)如果没有可以运行chmod 777 cpp.sh 添加可执行权限

  7. 零基础学python-1.5 第一个程序

    这一个章节我们来说说怎么建立一个python程序 1.打开idle 2.点击File->new file,然后会弹出一个编辑窗体 3.在编辑窗体里面输入命令代码 程序代码: print(&quo ...

  8. 非常easy学习的JQuery库 : (二) 选择器

    作用 选择器同意您对元素组或单个元素进行操作. 在前面的章节中,我们介绍了一些有关怎样选取 HTML 元素的实例. 关键点是学习 jQuery 选择器是怎样准确地选取您希望应用效果的元素. jQuer ...

  9. [Material Design] 教你做一个Material风格、动画的button(MaterialButton)

    原创作品,转载请注明出处:http://blog.csdn.net/qiujuer/article/details/39831451 前段时间Android L 公布了,相信看过公布会了解过的朋友都为 ...

  10. rm 命令简要

    rm   单独使用只能删除文件不能删除文件夹    rm -r 可以删除文件夹和文件 1.rm   test.txt   删除文件 2.rm   -r   test.txt   每次删除的时候都询问是 ...