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 ...
随机推荐
- 闲来无事做了一个批处理的win10账号管理
@echo off %1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe&q ...
- NodeJS初介
之前很多环境搭建中都使用到了Nodejs,所以这边对Nodejs做一个简单总结. 1.什么是Nodejs Node.js是一个Javascript运行环境(runtime),发布于2009年5月,由R ...
- vim编辑器的使用技巧
vim(vi)是上Linux非常常用的编辑器,很多Linux发行版都默认安装了vi(vim).vi(vim)命令繁多但是如果使用灵活之后将会大大提高效率.vi是“visual interface”的缩 ...
- K:java中properties文件的读写
Properties类与.properties文件: Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集的类,不过Properties有特殊 ...
- python实现二分查找算法
二分查找算法也成为折半算法,对数搜索算法,一会中在有序数组中查找特定一个元素的搜索算法.搜索过程是从数组中间元素开始的 如果中间元素正好是要查找的元素,则搜索过程结束:如果查找的数大于中间数,则在数组 ...
- 微信小程序开发之选项卡
选项卡是web开发中经常使用到的一个模块,在小程序中竟然没有,这里参考别人的文章自己做了一个双选项卡 实现思路: 通过绑定swichNav事件来控制currentTab(当前选项卡)和isShow(是 ...
- 深入剖析MSAA
本文打算对MSAA(Multisample anti aliasing)做一个深入的讲解,包括基本的原理.以及不同平台上的实现对比(主要是PC与Mobile).为了对MSAA有个更好的理解,所以写下了 ...
- mysql目录迁移 更改mysql的存储目录
元旦节刚过完回来,忙了一天,现在的时间剩余不是很充足,所以更新简短的文章一篇! 正文: 正常情况下mysql的存储目录都是在/var/lib/mysql/下的,那么怎么将存储位置改到/data_mys ...
- ThreadPool.QueueUserWorkItem引发的血案,线程池异步非正确姿势导致程序闪退的问题
ThreadPool是.net System.Threading命名空间下的线程池对象.使用QueueUserWorkItem实现对异步委托的先进先出有序的回调.如果在回调的方法里面发生异常则应用程序 ...
- mysql加锁读
Locking Reads 在同一个事务中,如果你先查询数据,随后对相关数据进行插入或修改,那么在标准的SLELECT中不会给出足够的保护.在你查询期间另一个事务可以更新或者删除相同的行.InnoDB ...