递归实现当然太简单,也用不着为了ac走这种捷径吧。。非递归实现还挺有意思的。
树的非递归遍历一定要借助栈,相当于把原来编译器做的事情显式的写出来。对于中序遍历,先要访问最左下的节点,一定是进入循环后,不断的往左下走,走到不能走为止,这时候,可以从栈中弹出访问的节点,相当于“左根右”过程的“根”,然后应该怎么做呢?想一下中序遍历完根节点之后应该干嘛,对,是走到右子树中继续重复这个过程,但是有一点,...
分类:
其他好文 时间:
2014-05-09 14:57:08
阅读次数:
211
这道题其实跟二叉搜索树没有什么关系,给定n个节点,让你求有多少棵二叉树也是完全一样的做法。思想是什么呢,给定一个节点数x,求f(x),f(x)跟什么有关系呢,当然是跟他的左右子树都有关系,所以可以利用其左右子树的结论,大问题被成功转化成了小问题。最熟悉的方法是递归和dp,这里显然有大量的重复计算,用dp打表好一些。
后来实验的同学说,这其实是一个Catalan数,上网查了一下,果然啊。Catal...
分类:
其他好文 时间:
2014-05-09 14:47:53
阅读次数:
229
可能没想到简单方法的人,在上一题中就把这一题给做了。怎样把所有的树都生成出来呢?方法肯定用的是递归,但是有个致命的问题,如果做好了根节点再递归,那么出来的很多树都公用一个根节点,结果肯定是乱七八糟的。
怎么做?其实做法跟之前求个数在思想上是高度统一的,先把所有的左右子树都求出来,然后把它们之间的所有组合都连接到一个新建立出来的根节点,既然是分开左右子树,很容易想到类似二分的思想,每次指定的不是一...
分类:
其他好文 时间:
2014-05-09 14:13:55
阅读次数:
376
Path Sum IGiven a binary tree and a sum,
determine if the tree has a root-to-leaf path such that adding up all the values
along the path equals the gi...
分类:
其他好文 时间:
2014-05-09 13:21:59
阅读次数:
320
If you ever had the problem where you need to
extract files from a SharePoint Content Database or normal SQL Database stored
as binary, this post will...
分类:
数据库 时间:
2014-05-09 09:54:59
阅读次数:
495
Given a binary tree, check whether it is a
mirror of itself (ie, symmetric around its center).For example, this binary tree
is symmetric: 1 / \ ...
分类:
其他好文 时间:
2014-05-09 09:28:18
阅读次数:
253
http://acm.hdu.edu.cn/showproblem.php?pid=1325
1 #include 2 #include 3 #include 4 #define maxn 5000 5 using namespace std; 6 7
int in[maxn]...
分类:
其他好文 时间:
2014-05-09 08:42:25
阅读次数:
316
这道题就是找规律啊!!!想想啊,11和10是可以连续的,那么10和11也是可以连续的。下面是AC代码:
1 /** 2 * The gray code is a binary numeral system where two successive values
differ in on...
分类:
其他好文 时间:
2014-05-09 07:38:17
阅读次数:
325
$per_len=20000;//每次读多少字节$no_read_len=$content_len;//未读的字节(总字节大小)$str=‘‘;while($len<$content_len){$read=socket_read($socket,$per_len,PHP_BINARY_READ);$str.=$read;$len+=strlen($read);//总共读了多少字节}http://hi.baidu.com/cuttinger/item/..
分类:
Web程序 时间:
2014-05-09 06:45:59
阅读次数:
566
最近在看这本书。不过发现需要安装编译工具 lex, yacc。书上说Linux自带lex, yacc。不过我发现我的没有。并且,ubuntu不使用lex, yacc。输入sudo apt-get install yacc lex
会报错, 报错内容如下:Reading package lists... Done
Building dependency tree
Reading sta...
分类:
其他好文 时间:
2014-05-09 06:18:48
阅读次数:
989