POJ1577 树的遍历
来源:互联网 发布:彩票控 知乎 编辑:程序博客网 时间:2024/06/07 18:43
注意建树方法, 不能像线段树一样用i<<1, i<<1|1, 因为这棵树可能不是完全二叉树, 所以会存在下标浪费。
#include<string.h>
#include<cstdio>#define MAXN 26
using namespace std;
struct Tree{
char v;
int lch, rch;//这个很重要, 不能像完全二叉树一样建树了, 极端情况有2^25的节点编号
void init(){ lch=rch=0; }
}tree[MAXN];
char s[MAXN][MAXN];
int cnt, e;
void build(int i, char c){
if (c<tree[i].v) {
if (tree[i].lch == 0){
tree[e].v=c;
tree[i].lch=e++;
}
else
build(tree[i].lch, c);
}
else {
if (tree[i].rch == 0){
tree[e].v=c;
tree[i].rch=e++;
}
else
build(tree[i].rch, c);
}
}
void tprint(int i){
printf("%c", tree[i].v);
if (tree[i].lch)
tprint(tree[i].lch);
if (tree[i].rch)
tprint(tree[i].rch);
}
int main(){
cnt=0;
while (scanf("%s", s[++cnt]) != EOF){
if (s[cnt][0]=='*' || s[cnt][0]=='$'){
for (int i=0; i<=MAXN; i++){
tree[i].init();
}
e=0;
tree[e++].v=s[cnt-1][0];
for (int i=cnt-2; i>=1; i--){
int len=strlen(s[i]);
for (int j=0; j<len; j++){
build(0, s[i][j]);
}
}
tprint(0);
printf("\n");
if(s[cnt][0] == '$') break;
cnt = 0;
}
}
return 0;
}
阅读全文
0 0
- POJ1577 树的遍历
- poj1577 二叉查找树复原,然后中序遍历输出
- 二叉树 poj1577
- poj1577
- POJ1577
- POJ1577 二叉树的创建及先序遍历(给定一些二叉树中的值,大于根的排在右边,小于根的排在左边)
- POJ1577 二叉排序树
- poj1577 Falling Leaves
- poj1577~Falling Leaves~二叉排序树~
- poj1577 Falling Leaves
- poj1577 二叉排序树基础练习题
- poj1577解题报告
- poj1577 Falling Leaves 二叉排序树
- 二叉树的遍历(层遍历和深度遍历)
- 二叉树的前序中序后序遍历,非递归遍历 层次遍历
- 二叉树的先中后序遍历,递归遍历,非递归遍历
- 二叉树遍历-----前序后序迭代遍历的新思路
- 二叉树的遍历-按层次遍历
- 每日一题(51)—— 大小端判断
- 笨办法学 Python · 续 练习 23:三叉搜索树
- sparkstreaming直连kafka Java实现 自己管理offset
- 最新 ARKit 演示:手指直接在空中作画
- Spring整合CXF webservice restful 实例
- POJ1577 树的遍历
- 翻转单词序列 vs 左旋转字符串
- 基础dp5
- 剑指offer——普通二叉树的子结构
- Jmeter之数据参数化篇
- 互联网项目架构经验分享
- 选择排序
- 不高兴的小明
- 极光IM cordova插件 jmessage-phonegap-plugin 各版本更新日志