hdu1710-Binary Tree Traversals (由二叉树的先序序列和中序序列求后序序列)

http://acm.hdu.edu.cn/showproblem.php?pid=1710

Binary Tree Traversals

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4210    Accepted Submission(s): 1908

Problem Description

A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can be systematically traversed or ordered. They are preorder, inorder and postorder. Let T be a binary tree with root r and subtrees T1,T2.

In a preorder traversal of the vertices of T, we visit the root r followed by visiting the vertices of T1 in preorder, then the vertices of T2 in preorder.

In an inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root r, followed by the vertices of T2 in inorder.

In a postorder traversal of the vertices of T, we visit the vertices of T1 in postorder, then the vertices of T2 in postorder and finally we visit r.

Now you are given the preorder sequence and inorder sequence of a certain binary tree. Try to find out its postorder sequence.

Input

The input contains several test cases. The first line of each test case contains a single integer n (1<=n<=1000), the number of vertices of the binary tree. Followed by two lines, respectively indicating the preorder sequence and inorder sequence. You can assume they are always correspond to a exclusive binary tree.

Output

For each test case print a single line specifying the corresponding postorder sequence.

Sample Input

9
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6

Sample Output

7 4 2 8 9 5 6 3 1

题解:意思很直白。给你二叉树的先序序列和中序序列,让你求后序序列。先利用先序序列和中序序列的特征递归建树,再后序遍历。

代码:

 1 #include <fstream>
 2 #include <iostream>
 3 #include <cstdio>
 4
 5 using namespace std;
 6
 7 const int N=1005;
 8 struct node{
 9     int c;
10     struct node *lch,*rch;
11 };
12 int n,tn;
13 int pre[N],in[N];
14
15 void createTree(int* l,int* r,int i,int j,int e,int f,node** tree);
16 void postOrder(node *p);
17
18 int main(){
19     //freopen("D:\\input.in","r",stdin);
20     //freopen("D:\\output.out","w",stdout);
21     node *tree;
22     while(~scanf("%d",&n)){
23         for(int i=0;i<n;i++)
24             scanf("%d",&pre[i]);
25         for(int i=0;i<n;i++)
26             scanf("%d",&in[i]);
27         createTree(pre,in,0,n-1,0,n-1,&tree);
28         tn=0;
29         postOrder(tree);
30     }
31     return 0;
32 }
33 void createTree(int* l,int* r,int i,int j,int e,int f,node** tree){//可以思考下这里为什么是**,可不可以用*?
34     int m;
35     (*tree)=new node;
36     (*tree)->c=l[i];
37     m=e;
38     while(r[m]!=l[i])    m++;
39     if(m==e)    (*tree)->lch=NULL;
40     else    createTree(l,r,i+1,i+m-e,e,m-1,&(*tree)->lch);
41     if(m==f)    (*tree)->rch=NULL;
42     else    createTree(l,r,i+m-e+1,j,m+1,f,&(*tree)->rch);
43 }
44 void postOrder(node *p){
45     if(p!=NULL){
46         postOrder(p->lch);
47         postOrder(p->rch);
48         printf("%d",p->c);
49         if(++tn<n)  printf(" ");
50         else    puts("");
51         delete p;
52     }
53 }
时间: 2024-10-18 14:18:00

hdu1710-Binary Tree Traversals (由二叉树的先序序列和中序序列求后序序列)的相关文章

hdu 1701 (Binary Tree Traversals)(二叉树前序中序推后序)

Binary Tree Traversals Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called

hdu1710(Binary Tree Traversals)(二叉树遍历)

Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3475    Accepted Submission(s): 1555 Problem Description A binary tree is a finite set of vertices that is either empty or

hdu1710 Binary Tree Traversals(二叉树的遍历)

A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can be systematically

HDU 1710 二叉树的遍历 Binary Tree Traversals

Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4330    Accepted Submission(s): 1970 Problem Description A binary tree is a finite set of vertices that is either empty or

HDU 1710 Binary Tree Traversals(二叉树)

题目地址:HDU 1710 已知二叉树先序和中序求后序. #include <stdio.h> #include <string.h> int a[1001], cnt; typedef struct node { int date ; node *lchild , *rchild ; }*tree; int getk(int ch,int ino[],int is,int n) { for(int i = is ; i <= is + n -1 ; i++) if(ino[

HDU-1701 Binary Tree Traversals

http://acm.hdu.edu.cn/showproblem.php?pid=1710 已知先序和中序遍历,求后序遍历二叉树. 思路:先递归建树的过程,后后序遍历. Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3442    Accepted Submission(s): 1541

hdu 1710 Binary Tree Traversals 前序遍历和中序推后序

题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4205    Accepted Submission(s): 1904 Problem Description A binary tree i

leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

【LeetCode-面试算法经典-Java实现】【226-Invert Binary Tree(反转二叉树)】

[226-Invert Binary Tree(反转二叉树)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun-Chao] 原题 Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 题目大意 将一棵二叉树进行翻转. 解题思路 对每一个结点,将它的左右子树进行交换,再对它的左右子结点进行同样的操作. 代码实现 树结点类 pub