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 ...
随机推荐
- TensorFlow 代码行统计
https://github.com/tensorflow/tensorflow.git
- python爬虫爬取人人车(二手车)、利用padas、matplotlib生成图表,将信息打成csv格式
该程序主要为了抓取人人车卖车信息,包括车系.车型号.购车日期.卖车价格.行驶路程.首付价格等等信息.话不多说直接代码. 入库之后将Mongodb里的信息导出成Excel语句 mongoexport - ...
- C# 调用动态链接库,给游览器写入Cookie
样例代码: class Program { /// <summary> /// 写 /// </summary> /// <param name="lpszUr ...
- 访问vm中centos的web站点
vm网络连接设置成NAT 需要把centos设置成静态IP 再不行,记得把centos的防火墙先关闭
- Git详解之二:Git基础
Git 基础 读完本章你就能上手使用 Git 了(伯乐在线注:如果你对Git还不了解,建议从本Git系列第一篇文章开始阅读).本章将介绍几个最基本的,也是最常用的 Git 命令,以后绝大多数时间里用到 ...
- 阿里云ECS搭建SVN配置外网
阿里云ECS搭建SVN后,配置外网启动不了,检查云服务器没发现问题,后来发现是阿里云拦截,需要在阿里云控制台ECS安全组新增如下配置:
- volatile关键字是如何起作用的?
关键字volatile是Java虚拟机提供的最轻量级的同步机制,但是在平时的项目里面,遇到需要多线程的时候更多地使用的是synchronized关键字来进行同步.个人而言,更多的原因是对volatil ...
- jQuery 选择器 (一)
选择器 实例 选取 * $("*") 所有元素 #id $("#lastname") id="lastname" 的元素 .class $( ...
- springMVC(7)---表单标签
springMVC(7)---表单标签 form标签作用 简单来讲form表单有两大作用 1:第一就是往后端提交数据或者前端回显 ...
- 文字太多?控件太小?试试 TextView 的新特性 Autosizeing 吧!
Hi,大家好,我是承香墨影! Android 8.0 已经发布了有一阵子了,如果你有在关注它,你应该会知道它新增了一个对于 TextView 字体大小变动的新特性:Autosizing. 本身这个新特 ...