相关概念:

一棵二叉搜索树(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. 解决zabbix“ZBX_NOTSUPPORTED: Timeout while executing a shell script”报错

    如题所示,在zabbix_server使用zabbix_get获取自定义“UserParameter”对应的脚本的数据时,出现了如题所示的报错信息 [root@nmp01 scripts]# /usr ...

  2. xcode 项目证书跟签名都正确的时候,还报证书错误

    原因,安装证书错误,导致无法匹配证书, 方案:删除原来的证书,重新安装 打开终端 1.cd Library/ 2.cd MobileDevice/ 3.open Provisioning\ Profi ...

  3. 01-2制作U盘启动盘--装机助理工具

    在可以上网的电脑上执行制作启动盘的操作: 打开浏览器输入:http://www.zhuangjizhuli.com/upan.html 1.制作U盘启动盘: 2.下载 装机助理 软件 3.步骤: 第一 ...

  4. nginx 做前端代理时proxy参数配置

    1.后台可登录: proxy_connect_timeout 300s; proxy_send_timeout ; proxy_read_timeout ; proxy_buffer_size 256 ...

  5. java之CGLIB动态代理

    © 版权声明:本文为博主原创文章,转载请注明出处 CGLIB动态代理: CGLIB动态代理就是对指定的类生成一个子类,覆盖其中所有的方法并环绕增强 优势: - 1. 业务类只需要关注业务逻辑本身,保证 ...

  6. JQuery小结(转)

    一.页面加载 JQ的页面加载比JS要快,当整个dom树结构生成完毕后就会加载 JQ页面加载不存在覆盖问题,加载的时候是顺序执行 JQ的页面加载最简写的方式为: $(function(){ alert( ...

  7. 高盛CEO致大学毕业生:要与有野心的人为伍

    我认为讲的非常棒.年轻人就要这样. 高盛集团首席运行官(CEO)劳尔德-贝兰克梵(Lloyd Blankfein)周四在曼哈顿贾维茨中心參加了拉瓜迪亚社区大学的第41届毕业典礼并发表演讲.在面向约10 ...

  8. 解决Incorrect integer value: &#39;&#39; for column &#39;id&#39; at row 1的方法

    在使用Navicat for MySQL还原数据库备份时.出现Incorrect integer value: '' for column 'id' at row 1的错误; 网上查资料发现5以上的版 ...

  9. kafka 配置kerberos校验以及开启acl实践

    转载请注明原创地址:http://www.cnblogs.com/dongxiao-yang/p/7131626.html kafka从0.9版本以后引入了集群安全机制,由于最近需要新搭建一套kafk ...

  10. u3D大场景的优化

    首先介绍下draw call(这个东西越少你的游戏跑的越快): 在游戏中每一个被展示的独立的部分都被放在了一个特别的包中,我们称之为“描绘指令”(draw call),然后这个包传递到3D部分在屏幕上 ...