相关概念:

一棵二叉搜索树(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. Gauge安装

    Gauge系列推荐:https://blog.csdn.net/column/details/20089.html Gauge是一种轻量级的跨平台测试自动化工具,可以以业务语言编写测试用例. 安装 下 ...

  2. 在服务端发起一个Post请求

    1.http://www.tuling123.com/openapi/api?key=9d2ff29d44b54e55acadbf5643569584&info=? 上面这个请求在服务端发起 ...

  3. Delphi Math里的基本函数,以及浮点数比较函数

    Delphi里的好东西太多,多到让人觉得烦.这种感觉就是当年打游戏<英雄无敌3>,改了钱以后,有钱了每天都要造建筑,明明是好事,可是让人觉得烦. 先记录下来,以后再回来加强对Math单元的 ...

  4. Python--多进程--01

    multiprocess import multiprocessing import time def worker_1(interval): print(' i am worker1') n=5 w ...

  5. linux rm -rf * 文件恢复记

    手太快,肠子都毁清了.本来是删除一个文件 rm path/myfile.txt结果不知为何加了个*,变成了rm path/myfile.txt *赶紧ls,发现所有代码都化为了乌有,还没提交,还没备份 ...

  6. HDU 5294 Tricks Device (最大流+最短路)

    题目链接:HDU 5294 Tricks Device 题意:n个点,m条边.而且一个人从1走到n仅仅会走1到n的最短路径.问至少破坏几条边使原图的最短路不存在.最多破坏几条边使原图的最短路劲仍存在 ...

  7. NFS详细分析

    1. NFS服务介绍 1.1什么是NFS服务 NFS(Network File System)即网络文件系统,它允许网络中的计算机之间通过TCP/IP网络共享资源.在NFS的应用中,本地NFS的客户端 ...

  8. C#多线程简单例子讲解

    C#多线程简单例子讲解 标签: 多线程c#threadobjectcallbacktimer 分类: C#(7) 转载网址:http://www.knowsky.com/540518.html .NE ...

  9. JS中关于闭包和this的指向

    闭包个人理解   函数内部还有一个函数,其作用就是可以访问上一层函数中的变量 下面的案例中函数内部有函数,this的指向就变为window了 结果闭包中this指向的两种方法 1.call对象冒充可以 ...

  10. python操作Excel读写--使用xlrd (转)

    (转自:http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html) 一.安装xlrd模块 到python官网下载http://pypi ...