题目
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 ...
分类:
其他好文 时间:
2014-06-08 17:06:58
阅读次数:
235
1. 递归解法
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
cl...
分类:
其他好文 时间:
2014-06-08 16:51:59
阅读次数:
199
本题有两个考点:
1 求逆序数的性质
计算逆序数的公式, 一个数arr[i]从前面放到后面,必然会有n-arr[i]-1个数比这个大,那么就有n-arr[i]-1个逆序数增加,同时因为前面少了个arr[i]数,那么就必然有arr[i]个(加上零)数比起小的数失去一个逆序数,总共失去arr[i]个逆序数,所以新的逆序数为增加了n-arr[i]-1-arr[i]个逆序数(当然有可能是减小了,视ar...
分类:
其他好文 时间:
2014-06-08 15:52:20
阅读次数:
275
【题目】
Follow up for problem "Populating Next Right Pointers in Each Node".
What if the given tree could be any binary tree? Would your previous solution still work?
Note:
You may only use constant extra space.
For example,
Given the following binary tre...
分类:
其他好文 时间:
2014-06-08 15:46:22
阅读次数:
303
题目
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 forward. Cou...
分类:
其他好文 时间:
2014-06-08 15:32:45
阅读次数:
245
extjs4 tree check 级联选择 实现效果:关键代码:
function changeAllNode(node, isCheck) {
allChild(node, isCheck);
allParent(node, isCheck);
function allChild(nodec, isCheckc) {
var chileNodes = n...
分类:
Web程序 时间:
2014-06-08 15:25:20
阅读次数:
222
问题:
给定二叉树的前序和中序遍历,重构这课二叉树.
分析:
前序、中序、后序都是针对于根结点而言,所以又叫作先根、中根、后根(当然不是高跟)。
前序:根 左 右
中序:左 根 右
对二叉树,我们将其进行投影,就会发现个有趣的事:
发现投影后的顺序,恰好是中序遍历的顺序,这也就是为什么在构造二叉树的时候,一定需要知道中序遍历,因为中序遍历决定了结点间的相对左右位置关系。所...
分类:
其他好文 时间:
2014-06-08 15:20:12
阅读次数:
214
题目
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 below.
...
分类:
其他好文 时间:
2014-06-08 15:15:26
阅读次数:
223
1. 递归解法
2. 非递归解法(空间复杂度O(n)和O(1))...
分类:
其他好文 时间:
2014-06-08 10:47:37
阅读次数:
139
问题:
由中序和后序遍历构造二叉树。
分析:
Construct Binary Tree from Preorder and Inorder
Traversal
//实现
TreeNode *addNode(vector &inorder, int s1, int end1, vector &postorder, int s2, int end2)
{
...
分类:
其他好文 时间:
2014-06-08 09:56:52
阅读次数:
206