Tree Recovery(由先、中序列构建二叉树)
题目来源:
http://poj.org/problem?id=2255
题目描述:
Description
This is an example of one of her creations:
D
/ \
/ \
B E
/ \ \
/ \ \
A C G
/
/
F
To record her trees for future generations, she wrote down two
strings for each tree: a preorder traversal (root, left subtree, right
subtree) and an inorder traversal (left subtree, root, right subtree).
For the tree drawn above the preorder traversal is DBACEGF and the
inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).
Now, years later, looking again at the strings, she realized that
reconstructing the trees was indeed possible, but only because she never
had used the same letter twice in the same tree.
However, doing the reconstruction by hand, soon turned out to be tedious.
So now she asks you to write a program that does the job for her!
Input
Each test case consists of one line containing two strings preord
and inord, representing the preorder traversal and inorder traversal of a
binary tree. Both strings consist of unique capital letters. (Thus they
are not longer than 26 characters.)
Input is terminated by end of file.
Output
each test case, recover Valentine's binary tree and print one line
containing the tree's postorder traversal (left subtree, right subtree,
root).
Sample Input
DBACEGF ABCDEFG
BCAD CBAD
Sample Output
ACBFGED
CDAB
解题思路:
根据给出的先根序列和中根序列构建二叉树后,后序遍历二叉树即可。
拿第一个样例来说,现在先根序列中找到根节点D,然后在中根序列中找到D,可以得到以D为根节点的二叉树的左子树有ABC,以D为根节点的二叉树的右子树有EFG。接下来在先根序列中找到B,再在中根序列中将以B为根节点的二叉树
的左右子树找到,如此递归,直至将序列处理完毕。
代码实现:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node{
char ch;
struct node *left,*right;
};
struct node* creat(char *pre,char *in,int len);
void post(struct node* head);
int main()
{
char pre[],in[];
struct node *head;
head = ( struct node *)malloc(sizeof(struct node));
while(scanf("%s %s",pre,in) != EOF)
{
int len=strlen(pre);
head=creat(pre,in,len); post(head);//后序遍历
printf("\n");
}
return ;
}
struct node* creat(char *pre,char *in,int len)
{
if(len==)
return NULL; struct node *head;
head = (struct node*)malloc(sizeof(struct node));
head->ch=pre[]; char *p;
for(p=in;p != NULL;p++)//指针字符串中空为结束标志
if(*p == *pre)
break; int k=p-in;
head->left=creat(pre+,in,k);
head->right=creat(pre+k+,p+,len-k-);
return head;
}
void post(struct node* head)
{
if(head == NULL)
return;
post(head->left);
post(head->right);
printf("%c",head->ch);
}
易错分析:
注意注释,指针字符串和字符串数组还是有一定区别的,比如结束标志位NULL
Tree Recovery(由先、中序列构建二叉树)的更多相关文章
- Construct Binary Tree from Preorder and Inorder Traversal(根据前序中序构建二叉树)
根据前序中序构建二叉树. 1 / \ 2 3 / \ / \ 4 5 6 7对于上图的树来说, index: 0 1 2 3 4 5 6 先序遍历为: 6 3 7为了清晰表示,我给节点上了颜色,红色是 ...
- LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 已知前序(后序)遍历序列和中序遍历序列构建二叉树(Leetcode相关题目)
1.文字描述: 已知一颗二叉树的前序(后序)遍历序列和中序遍历序列,如何构建这棵二叉树? 以前序为例子: 前序遍历序列:ABCDEF 中序遍历序列:CBDAEF 前序遍历先访问根节点,因此前序遍历序列 ...
- Java 重建二叉树 根据前序中序重建二叉树
题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2, ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历构建二叉树)
根据中序和后续遍历构建二叉树. /** * Definition for a binary tree node. * public class TreeNode { * int val; * Tree ...
- [Swift]LeetCode105. 从前序与中序遍历序列构造二叉树 | Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- LeetCode: 106_Construct Binary Tree from Inorder and Postorder Traversal | 根据中序和后序遍历构建二叉树 | Medium
要求:根据中序和后序遍历序列构建一棵二叉树 代码如下: struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int ...
- LeetCode:105_Construct Binary Tree from Preorder and Inorder Traversal | 根据前序和中序遍历构建二叉树 | Medium
要求:通过二叉树的前序和中序遍历序列构建一颗二叉树 代码如下: struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode ...
随机推荐
- 自动化测试辅助工具(Selenium IDE等)
本随表目录 Selenium IDE安装和使用 FireBug安装和使用 FirePath安装和使用 Selenium IDE安装 方式一:打开Firefox-->添加组件-->搜索出 ...
- Git操作流程,基本命令演示
任务列表: 有一个中央库Center,和三个工作站A,B,C. 初始化时,代码存放在中央库中,A,B,C三个工作站开始工作之前都要首先从中央库克隆一份代码到本地. 第一个任务:A和B合作修复一个缺陷, ...
- ABP架构学习系列一 整体项目结构及目录
本系列是基于aspnetboilerplate-0.8.4.0版本写的,其中原因是由于较高的版本太抽象难以理解和分析,对于还菜菜的我要花更多的时间去学习. abp的源码分析学习主要来源于 HK Zha ...
- Head First设计模式之中介者模式
一.定义 又称为调停者模式,定义一个中介对象来封装系列对象之间的交互.中介者使各个对象不需要显示地相互引用,从而使其耦合性松散,而且可以独立地改变他们之间的交互. 二.结构 组成: ● 抽象中介者(M ...
- ant安装和验证
1.下载apache-ant-1.9.6 2.D:\software\apache-ant-1.9.6 3.配置环境变量 在系统变量path的最后面添加D:\software\apache-ant-1 ...
- Android 获取唯一标识替代方法
private static String getTheOnlyID() { String onlyOne; //获取IMEI TelephonyManager TelephonyMgr = (Tel ...
- AutoMapper 使用总结
初识AutoMapper 在开始本篇文章之前,先来思考一个问题:一个项目分多层架构,如显示层.业务逻辑层.服务层.数据访问层.层与层访问需要数据载体,也就是类.如果多层通用一个类,一则会暴露出每层的字 ...
- J2EE 项目 org.apache.jasper.JasperException: 解决方法
项目从一个电脑转移到另一台电脑总是有各种意外qaq~ 刚放假把从实验室的项目拷回自己的电脑回家继续coding,结果出了这个错误.... 各个地方都调试原来是Tomcat版本问题!!!我电脑上的是6. ...
- 一种基于http协议的敏感数据传输方案
最近公司需要通过公网与其它平台完成接口对接,但是基于开发时间和其它因素的考虑,本次对接无法采用https协议实现.既然不能用https协议,那就退而求其次采用http协议吧! 那么问题来了!在对接的过 ...
- docker with flannel
** 原创文章,请勿转载 ** docker的单host,多container环境下,是使用host的docker0网桥进行通信的.如果跨host, container之间要通信怎么办呢?答案是fla ...