Given n, how many structurally unique BST's (binary search trees) that store values 1...n?
For example,
Given n = 3, there are a total of 5 unique BST's.
1 3 3 2 1
\ ...
分类:
其他好文 时间:
2015-06-23 15:43:19
阅读次数:
86
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
Calling next() will return the next smallest number in the BST.
Note: next()...
分类:
其他好文 时间:
2015-06-23 13:38:01
阅读次数:
103
Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...n?For example,Givenn= 3, there are a total of 5 unique BST's. 1.....
分类:
其他好文 时间:
2015-06-17 23:06:50
阅读次数:
189
给定二叉查找树中的两个节点,求它们的最近公共祖先(Lowest Common Ancestor - LCA)。
在详细介绍之前,可以先参考下这篇文章"二叉树(70) - 最近公共祖先[1]"
函数原型定义如下:
Node *getLCA(Node* root, int n1, int n2)
其中n1和n2是指定的两个节点值。
例如, 上述BST中,10和1...
分类:
其他好文 时间:
2015-06-16 01:22:54
阅读次数:
201
在前一篇文章中,讨论了二叉查找树的查找以及插入操作。本篇文章主要讨论删除操作。
当删除一个节点时,可能包含下面的这些情形:
1) 被删节点为叶子:
这种情况下,简单的将叶子删除即可。
50 50
/ \ delete(2...
分类:
其他好文 时间:
2015-06-16 01:22:47
阅读次数:
122
假设树的节点定义如下,查找一个指定值的前驱以及后继节点。如果树中没有找到指定值,则返回它所在区间的边界值。
struct
Node
{
int
key;
Node *left,*right ;
};
下面是实现此操作的算法,采用递归:
输入: 根节点, 键值
输出: 前驱节点,后继节点
1. If root is NUL...
分类:
其他好文 时间:
2015-06-16 01:22:37
阅读次数:
161
在本系列的第一篇文章中,已经介绍过了二叉查找树的一些性质:
节点的左子树中任意节点值小于根节点节点的右子树中任意节点值大于根节点左右子树都必须是二叉查找树,不允许存在重复节点。
基于上面的这些性质,自然的就得到了这种判断方式:树中的每个节点都有一个特定的值。
假设树的节点定义为:
struct Node
{
int key;
Node...
分类:
其他好文 时间:
2015-06-16 01:21:58
阅读次数:
119
Validate Binary Search TreeGiven a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than...
分类:
其他好文 时间:
2015-06-13 14:21:53
阅读次数:
103
Given n, how many structurally unique BST’s (binary search trees) that store values 1…n?For example,
Given n = 3, there are a total of 5 unique BST’s. 1 3 3 2 1
\ /...
分类:
其他好文 时间:
2015-06-13 09:58:12
阅读次数:
104
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next() and hasN...
分类:
其他好文 时间:
2015-06-13 09:56:01
阅读次数:
100