摘要: 先构建一个表达式树,构建方式:从右向左读字符串,如果该字符是大写字母,则构建右子树,接着构建左子树,用递归进行实现。接着层次遍历该树,把得到的结果逆序输出。 代码如下:View Code 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <queue> 5 #include <cctype> 6 using namespace std; 7 8 const int maxn = 10000+10; 9 10 struct Node11 {1 阅读全文
posted @ 2013-04-24 17:40 xiaobaibuhei 阅读(303) 评论(0) 推荐(0)
摘要: 《算法竞赛入门经典》6.3.3的题,给出一个二叉树的先序遍历和中序遍历,求它的后续遍历。 书中代码对递归的运用,没得说!递归是一个神奇的东西,我现在只是知道它,但却永不好它,还需努力啊。 代码如下:View Code 1 #include <cstdio> 2 #include <cstring> 3 4 const int maxn = 30; 5 6 void build(int n, char *s1, char *s2, char *s) 7 { 8 if(n <= 0) return; 9 int p = strchr(s2, s1[0]) - s2.. 阅读全文
posted @ 2013-04-24 16:25 xiaobaibuhei 阅读(352) 评论(0) 推荐(0)
摘要: 《算法竞赛入门经典》6.3.2的题,关于构建二叉树和层次遍历的问题。 书中的代码定义的节点结构用了一个have_value标记判断是否已有值,因为节点的值都是正整数,可以通过初始化为0来判断是否该节点已有值,另外,ans数组的n应在每次调用bfs函数时进行初始化。 第一次提交的时候PE了,原来是要去掉层次遍历结果的后缀空白符,可是我并没有在题目中找到相关的要求呀... 代码如下:View Code 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 5 const int maxn 阅读全文
posted @ 2013-04-24 15:49 xiaobaibuhei 阅读(400) 评论(0) 推荐(0)