1507: 二叉树的先序建树和后序输出
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 156 Solved: 98
[Submit][Status][Web Board]
Description
二叉树的先序建树和后序输出
Input
输入一行只包含大写字母的字符串,字符串长度小于100,#表示空节点,请按照先序遍历,输出后序遍历
Output
输出后序遍历,注意不要输出空节点,序列末尾不要输出空格
Sample Input
AB#DE###C#F##
Sample Output
EDBFCA
【分析】模板题(模板戳这)
#include<bits/stdc++.h>
using namespace std;
typedef struct node{
char val;
struct node *left;
struct node *right;
}node,*tree;
void build(tree &T)
{
char c;
cin>>c;
if(c=='#')T=NULL;
else {
T=new node;
T->val=c;
build(T->left);
build(T->right);
}
}
void post(tree T)
{
if(T)
{
post(T->left);
post(T->right);
cout<<T->val;
}
}
int main()
{
tree T=NULL;
build(T);
post(T);
return 0;
}