HDU 1710 Binary Tree Traversals

题意:给出一颗二叉树的前序遍历和中序遍历,输出其后续遍历

首先知道中序遍历是左子树根右子树递归遍历的,所以只要找到根节点,就能够拆分出左右子树

前序遍历是按照根左子树右子树递归遍历的,那么可以找出这颗树的根节点,

然后拆分出左右子树,对左右子树进行相同的操作,也就是将建树的这个函数递归调用下去

build函数还是理解了好久啊话说= =仍然是学习的代码

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include <cmath>
 5 #include<algorithm>
 6 using namespace std;
 7
 8 typedef long long LL;
 9 const int maxn=1005;
10
11 void build(int n,int *s1,int *s2,int *s){
12     if(n<=0) return;
13     int p;
14     for(int i=0;i<n;i++){
15         if(s2[i]==s1[0]){
16             p=i;//在中序遍历中找到根结点的位置
17             break;
18         }
19     }
20
21     build(p,s1+1,s2,s);//p为左子树的区间长度,s1+1是在前序遍历中左子树的开头,s2是在中序遍历中左子树的开头
22     build(n-p-1,s1+p+1,s2+p+1,s+p);//n-p-1为右子树的区间长度,s1+p+1 是在前序遍历中右子树的开头,s2是在中序遍历中右子树的开头
23     s[n-1]=s1[0];
24 }
25
26 int main()
27 {
28     int n,i,ans[maxn],s1[maxn],s2[maxn];
29     while(scanf("%d",&n)!=EOF){
30         for(i=0;i<n;i++) scanf("%d",&s1[i]);
31         for(i=0;i<n;i++) scanf("%d",&s2[i]);
32         build(n,s1,s2,ans);
33         for(i=0;i<n-1;i++) printf("%d ",ans[i]);
34         printf("%d\n",ans[i]);
35     }
36     return 0;
37 }

时间: 2024-10-07 05:02:50

HDU 1710 Binary Tree Traversals的相关文章

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

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 1710 Binary Tree Traversals

acm.hdu.edu.cn/showproblem.php?pid=1710 [题意] 给定一棵二叉树的前序遍历和中序遍历,输出后序遍历 [思路] 根据前序遍历和中序遍历递归建树,再后续遍历输出 malloc申请空间在堆,函数返回,内存不释放,需要free手动释放 [Accepted] #include<iostream> #include<cstdio> #include<string> #include<cstring> #include<alg

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

HDOJ 题目1710 Binary Tree Traversals(二叉搜索树)

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

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-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

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

Binary Tree Traversals HDU - 1710 ?

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