问题描述针对有序的数组或链表,要转为相对平衡的BST树时,通常有多种做法。
1. 对于有序的数组A,转为BST时,取数组中间元素A[m],转为根,然后递归转换A[1..m-1], A[m+1 .. n]即可。时间复杂度 T(n) = 2 * T(n/2) + O(1),得出T(n) = O(n)
2. 针对有序的链表,如果依据上述方法,也可以进行。然而要取到A[m]的话,则需要遍历n/2长度...
分类:
其他好文 时间:
2015-04-16 01:33:11
阅读次数:
171
题目链接:Unique Binary Search Trees
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 ...
分类:
其他好文 时间:
2015-04-14 21:41:56
阅读次数:
139
题目链接:Validate Binary Search Tree
Given 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 ...
分类:
其他好文 时间:
2015-04-14 21:41:20
阅读次数:
118
题目链接:Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
Note:
A solution using O(n) space is pretty stra...
分类:
其他好文 时间:
2015-04-14 21:39:52
阅读次数:
189
树的结构,如果不能保持平衡,那么其搜索性能会大大打折扣,而本节课介绍了几种经典的平衡树,如AVL,2-3-4tree,红黑树等等,然后着重讲了红黑树,接下来就红黑树的基本性质,作一些简短的总结。
首先,红黑树除了具有BST的基本性质外,还额外拥有以下的五大基本性质:
1)每个结点有一个色域,一个结点要么为黑结点,要么为红结点
2)根节点为黑结点
3)每个叶子结点都为黑结点(无键值...
分类:
编程语言 时间:
2015-04-14 21:37:52
阅读次数:
159
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
这道题要求根据一个有序的数组建立一棵二叉排序树。利用折半的思想,构造根节点,再递归构造左右子树。递归出口是数组中只含一个元素。TreeNode *sortedArrayToBST(vector &...
分类:
其他好文 时间:
2015-04-14 21:34:22
阅读次数:
98
问题描述BST树的遍历问题常常遇到,前序、中序、后序等。如果用递归的话,是非常方便的,其时间复杂度是O(n),空间复杂度是O(log n)级别。PS:stackoverflow问答网站上有一个问题指出,这类问题的复杂度不应该直接说是O(log n),因为编译器会进行一些优化,比如修改成尾递归等。不过我们这里暂时不考虑优化,从程序逻辑上来讲,BST递归遍历认为是O(log n)的复杂度。OK,那么如果...
分类:
其他好文 时间:
2015-04-14 21:33:57
阅读次数:
177
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...
分类:
其他好文 时间:
2015-04-14 13:01:30
阅读次数:
148
二分搜索
二叉搜索树
1 二叉搜索树的初始化插入搜索
2 使用BST排序
3 BST上根节点的插入insert
4 BST上的选择select操作划分操作
5 BST上的删除delete操作
6 两棵BST的连接join
BST的优缺点
参考资料和所有代码
1. 二分搜索将分治法应用于基于数组符号表的顺序搜索中,可以大大降低大型数据集合的搜索时间。
把数据集合分成两部分,确定搜索关键字属于哪一部分...
分类:
其他好文 时间:
2015-04-14 11:14:38
阅读次数:
207
Convert Sorted Array to Binary Search TreeGiven an array where elements are sorted in ascending order, convert it to a height balanced BST.很简单的二分法,只要给...
分类:
编程语言 时间:
2015-04-14 07:09:13
阅读次数:
133