Given an array where elements are sorted in ascending order, convert it to a height balanced BST.题目大意:给定一个升序序列的数组,将其转换为二叉搜索树。解题思路:数组中间元素是根元素,根元素将数组划分为...
分类:
其他好文 时间:
2015-04-20 22:39:28
阅读次数:
207
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.采用二分递归。 1 class Solution { 2 public: 3 TreeNode *...
分类:
其他好文 时间:
2015-04-19 21:10:54
阅读次数:
173
问题描述我们知道,Treap可以完成节点的动态插入、删除、查询,其每个操作的时间复杂度是O(log n),因为其实现较红黑树更为简单,因此常常用于某些场合,以替换红黑树的实现。Treap的每个节点维护了key, priority。struct Node {
int key;
int priority;
Node (int k, int p): key(k), priority...
分类:
其他好文 时间:
2015-04-19 18:00:03
阅读次数:
264
problem:
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 keys less th...
分类:
其他好文 时间:
2015-04-17 18:28:13
阅读次数:
142
题目链接:Convert Sorted List to
Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
这道题的要求是将有序链表转化成高度平衡的二叉搜索树(BST)。
1. 利...
分类:
其他好文 时间:
2015-04-17 18:16:32
阅读次数:
165
题目链接:Convert Sorted Array
to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
这道题的要求是将有序数组转化成高度平衡的二叉搜索树(BST)。
由于数组有序,因此相当于二叉搜索树...
分类:
其他好文 时间:
2015-04-17 18:15:21
阅读次数:
187
problem:
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 straight forwar...
分类:
其他好文 时间:
2015-04-17 18:14:49
阅读次数:
148
problem:
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 ...
分类:
其他好文 时间:
2015-04-17 09:45:12
阅读次数:
108
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 has...
分类:
其他好文 时间:
2015-04-16 17:45:41
阅读次数:
151
problem:
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.
For example,
Given n = 3, your program should return all 5 unique BST's shown bel...
分类:
其他好文 时间:
2015-04-16 12:34:36
阅读次数:
111