Tree Recovery(由先、中序列构建二叉树)

题目来源:

http://poj.org/problem?id=2255

题目描述:

Description

Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.
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

The input will contain one or more test cases.

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

For
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为根节点的二叉树的左右子树找到,如此递归,直至将序列处理完毕。代码实现:
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 struct node{
 5     char ch;
 6     struct node *left,*right;
 7 };
 8 struct node* creat(char *pre,char *in,int len);
 9 void post(struct node* head);
10 int main()
11 {
12     char pre[30],in[30];
13     struct node *head;
14     head = ( struct node *)malloc(sizeof(struct node));
15     while(scanf("%s %s",pre,in) != EOF)
16     {
17         int len=strlen(pre);
18         head=creat(pre,in,len);
19
20         post(head);//后序遍历
21         printf("\n");
22     }
23     return 0;
24 }
25 struct node* creat(char *pre,char *in,int len)
26 {
27     if(len==0)
28         return NULL;
29
30     struct node *head;
31     head = (struct node*)malloc(sizeof(struct node));
32     head->ch=pre[0];
33
34     char *p;
35     for(p=in;p != NULL;p++)//指针字符串中空为结束标志
36         if(*p == *pre)
37             break;
38
39     int k=p-in;
40     head->left=creat(pre+1,in,k);
41     head->right=creat(pre+k+1,p+1,len-k-1);
42     return head;
43 }
44 void post(struct node* head)
45 {
46     if(head == NULL)
47         return;
48     post(head->left);
49     post(head->right);
50     printf("%c",head->ch);
51 } 

易错分析:

注意注释,指针字符串和字符串数组还是有一定区别的,比如结束标志位NULL

原文地址:https://www.cnblogs.com/wenzhixin/p/8343860.html

时间: 2024-07-28 18:13:59

Tree Recovery(由先、中序列构建二叉树)的相关文章

Leetcode:Construct Binary Tree 前序和中序、后序和中序构建二叉树

前序和中序构建二叉树 后序和中序构建二叉树 分析:主要思路就是 在中序中找根节点然后划分左右子树,具体如下: 1. 查找根节点. 我们知道前序序列的第一个元素 和 后序序列的最后一个元素 肯定是根节点,我们就以此为突破口 2. 确定根节点的坐标. 我们在 中序序列中找到 根节点 的下标. 3. 分割左右子树. 对于中序序列:根节点下标之前的都属于左子树,之后的都属于右子树 对于先序序列:根节点之后的 一段元素(该段长度可以由中序序列的左子树长度确定)属于左子树,左子树之后的元素属于右子树 对于先

根据先序和中序序列构建二叉树

说明: 本次实验利用中序和先序序列,采用递归方式来构建二叉树 . 经过几天的失败和思考,我认为递归构建二叉树的过程中最重要的是递归单元,最麻烦的是递归参数的选择和传递. 简单将算法过程用如下流程图来表示:(本帖所用算法及图片均为原创内容,转贴注明出处) 算法:1.根据先序序列,建立根结点T 2.寻找中序序列的根结点位置,并据此位置计算左子树和右子树的区间 3.判断l_start和r_end是否相等,相等则表示只有一个根结点,设置其左右孩子结点为空并结束这一层:若不相等则继续下面步骤: 4.根据2

poj2255 Tree Recovery(求后续遍历,二叉树)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=2255 Description Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letter

根据前序和中序列 重建二叉树

输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并输出它的后序遍历序列. 输入: 输入可能包含多个测试样例,对于每个测试案例, 输入的第一行为一个整数n(1<=n<=1000):代表二叉树的节点个数. 输入的第二行包括n个整数(其中每个元素a的范围为(1<=a<=1000)):代表二叉树的前序遍历序列.

UVa 536 Tree Recovery(先序,中序求后序)

题意  给你二叉树的先序序列和中序序列  求它的后序序列 先序序列的第一个一定是根  中序序列根左边的都属于根的左子树  右边的都属于右子树  递归建树就行了 #include <bits/stdc++.h> using namespace std; typedef struct TNode { char data; TNode *lc, *rc; } node, *BTree; void build(BTree &t, string pre, string in) { t = new

LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)

Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively?中序遍历二叉树,递归遍历当然很容易,题目还要求不用递归,下面给出两种方法: 递归: 1 /**

题目:1385 由前序和中序构建二叉树

http://ac.jobdu.com/problem.php?pid=1385 蛮怀旧的题目,记得大一就见过一直没做过,没难度,纯小心吧. 类似的是有中序和后续构建二叉树.比如http://www.cnblogs.com/kaituorensheng/p/3788533.html 思路很简单 递归构造: #include <cstdio> #include <cstring> #include <iostream> #include <cstdio> #i

华中尔科技大机试 二叉树遍历 Easy *两序列构建二叉树

基本思想: 要求用两个序列构建新的二叉树,标准写法,注意下: 关键点: 无: #include<stdio.h> #include<stdlib.h> #include<iostream> #include<string> #include<vector> #include<algorithm> #include<map> #include<set> using namespace std; string s1

已知前序(后序)遍历序列和中序遍历序列构建二叉树(Leetcode相关题目)

1.文字描述: 已知一颗二叉树的前序(后序)遍历序列和中序遍历序列,如何构建这棵二叉树? 以前序为例子: 前序遍历序列:ABCDEF 中序遍历序列:CBDAEF 前序遍历先访问根节点,因此前序遍历序列的第一个字母肯定就是根节点,即A是根节点:然后,由于中序遍历先访问左子树,再访问根节点,最后访问右子树,所以我们找到中序遍历中A的位置,然后A左边的字母就是左子树了,也就是CBD是根节点的左子树:同样的,得到EF为根节点的右子树. 将前序遍历序列分成BCD和EF,分别对左子树和右子树应用同样的方法,