首页 技术 正文
技术 2022年11月12日
0 收藏 421 点赞 2,246 浏览 1909 个字

题目1078:二叉树遍历

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:5326

解决:3174

题目描述:

二叉树的前序、中序、后序遍历的定义:
前序遍历:对任一子树,先访问跟,然后遍历其左子树,最后遍历其右子树;
中序遍历:对任一子树,先遍历其左子树,然后访问根,最后遍历其右子树;
后序遍历:对任一子树,先遍历其左子树,然后遍历其右子树,最后访问根。
给定一棵二叉树的前序遍历和中序遍历,求其后序遍历(提示:给定前序遍历与中序遍历能够唯一确定后序遍历)。

输入:

两个字符串,其长度n均小于等于26。
第一行为前序遍历,第二行为中序遍历。
二叉树中的结点名称以大写字母表示:A,B,C….最多26个结点。

输出:

输入样例可能有多组,对于每组测试样例,
输出一行,为后序遍历的字符串。

样例输入:
ABC
BAC
FDXEAG
XDEFAG
样例输出:
BCA
XEDGAF分析:先递归重建二叉树,在递归后序遍历。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include <string>
using namespace std; typedef struct Node{
char data;
struct Node *lchild;
struct Node *rchild;
}BTree; string str1, str2;
void postOrder(BTree *T)
{
if(T!=NULL)
{if(T->lchild!=NULL)
postOrder(T->lchild);
if(T->rchild!=NULL)
postOrder(T->rchild);
cout<<T->data;
}
return;
} BTree *trans(int l1,int h1,int l2,int h2){
if(l1 > h1 || l2 > h2)//递归出口,不符合条件,直接返回空节点
return NULL;
int j = l2; while(str2[j] != str1[l1]){ j++; } //构造根节点
BTree *T=(BTree*)malloc(sizeof(BTree));
T->data=str2[j];
T->lchild=NULL;
T->rchild=NULL; T->lchild=trans(l1+,l1+j-l2,l2,j-); //返回左子树
T->rchild=trans(l1+j-l2+,h1,j+,h2); //返回右子树
return T;
} int main()
{ while(cin >> str1 >> str2)
{
int L1 = str1.length();
int L2 = str2.length();
BTree *T = trans(,L1-,,L2-);
postOrder(T);
cout << endl;
}
return ;
}
 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; typedef struct Node{
char data;
struct Node *lchild;
struct Node *rchild;
}BTree; char str1[], str2[]; void postOrder(BTree *T){
if(T != NULL){
if(T->lchild != NULL)
postOrder(T->lchild);
if(T->rchild != NULL)
postOrder(T->rchild);
printf("%c", T->data);
}
return;
} BTree *trans(int l1,int h1,int l2,int h2){
if(l1 > h1 || l2 > h2)//递归出口,不符合条件,直接返回空节点
return NULL;
int j = l2; while(str2[j] != str1[l1]){ j++; } //构造根节点
BTree *T = (BTree*)malloc(sizeof(BTree));
T->data = str2[j];
T->lchild = NULL;
T->rchild = NULL; T->lchild = trans(l1+, l1+j-l2, l2, j-); //返回左子树
T->rchild = trans(l1+j-l2+, h1, j+, h2); //返回右子树
return T;
} int main()
{
int L1, L2;
while(scanf("%s %s", str1, str2) != EOF){
L1 = strlen(str1);
L2 = strlen(str2);
BTree *T = trans(,L1-,,L2-);
postOrder(T);
printf("\n");
}
return ;
}
 
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297