遍历二叉树
来源:互联网 发布:前田阳菜 pk 樱井知香 编辑:程序博客网 时间:2024/06/06 01:57
题目描述
已知二叉树的一个按先序遍历输入的字符序列,如abc,,de,g,,f,,, (其中,表示空结点)。请建立二叉树并按中序和后序的方式遍历该二叉树。
输入
连续输入多组数据,每组数据输入一个长度小于50个字符的字符串。
输出
每组输入数据对应输出2行:
第1行输出中序遍历序列;
第2行输出后序遍历序列。
示例输入
abc,,de,g,,f,,,
示例输出
cbegdfa
cgefdba
提示
#include <stdio.h>
#include <stdlib.h>
struct node
{
char data;
struct node*l,*r;
};
int i;
char s[60];
struct node*creat(struct node*t)
{
if(s[i]==',')
{
t=NULL;
i++;
}
else
{
t=(struct node*)malloc(sizeof(struct node));
t->data=s[i];
i++;
t->l=creat(t->l);
t->r=creat(t->r);
}
return t;
}
void inorder(struct node*t)
{
if(t!=NULL)
{
inorder(t->l);
printf("%c",t->data);
inorder(t->r);
}
}
void paorder(struct node*t)
{
if(t!=NULL)
{
paorder(t->l);
paorder(t->r);
printf("%c",t->data);
}
}
int main()
{
struct node*t;
while(gets(s)!=NULL)
{ i=0;
t=creat(t);
inorder(t);
printf("\n");
paorder(t);
printf("\n");
}
return 0;
}
- 二叉树遍历、分层遍历
- 遍历二叉树--二叉树
- 【树】遍历二叉树
- 二叉树遍历
- 二叉树遍历
- 二叉树的遍历
- (原创)遍历二叉树
- 二叉树遍历-php
- 查找--遍历二叉树
- 遍历二叉树
- 二叉树遍历规则
- 二叉树的遍历
- 二叉树遍历
- 遍历二叉树
- 二叉树遍历问题
- 二叉树遍历
- 二叉树遍历(zz)
- 二叉树的遍历
- jquery 技巧
- 行动星计划
- EGORefreshTableHeaderView 解读
- 【POJ】[2367]Genealogical tree
- 搜索引擎中开发diversity功能时遇到的一个问题。
- 遍历二叉树
- 350. Intersection of Two Arrays II*
- MarginLayoutParams--一个可以在代码中直接设置margin的方法
- openCV for Python(一)——基础记录
- HDU1023——Train Problem II(卡特兰数)
- 【Unity】整理新版动画使用
- 【数据结构】后缀数组
- ORACLEインポート・エクスポート・バックアップ
- 单调栈:(修筑绿化带)