题目地址: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[i]==ch)return i;
}
void gettree(tree &t,int pre[],int ino[],int ps,int is,int n)
{
if(n==0) t = NULL;
else
{
t = new node;
t->date = pre[ps];
int k = getk(pre[ps],ino,is,n);
if(k == is) t->lchild = NULL;
else
gettree(t->lchild,pre,ino,ps+1,is,k-is);
if(k == is+n-1) t->rchild = NULL;
else
gettree(t->rchild,pre,ino,ps+1+(k-is),k+1,n-1-(k-is));
}
}
void pritree1(tree t)
{
if(t)
{
pritree1(t->lchild);
pritree1(t->rchild);
a[cnt++]=t->date;
}
}
int main()
{
int i, n, t;
int pre[1001] , ino[1001] ;
while(scanf("%d", &n)!=EOF)
{
cnt=0;
tree head;
for(i=0; i<n; i++)
{
scanf("%d",&pre[i]);
}
for(i=0; i<n; i++)
{
scanf("%d",&ino[i]);
}
gettree(head,pre,ino,0,0,n);
pritree1(head);
for(i=0; i<cnt-1; i++)
printf("%d ",a[i]);
printf("%d\n",a[cnt-1]);
}
return 0;
}

HDU 1710 Binary Tree Traversals(二叉树)的更多相关文章

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

    题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (J ...

  2. HDU 1710 Binary Tree Traversals (二叉树遍历)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  3. HDU 1710 Binary Tree Traversals(树的建立,前序中序后序)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  4. HDU 1710 Binary Tree Traversals(二叉树遍历)

    传送门 Description A binary tree is a finite set of vertices that is either empty or consists of a root ...

  5. 【二叉树】hdu 1710 Binary Tree Traversals

    acm.hdu.edu.cn/showproblem.php?pid=1710 [题意] 给定一棵二叉树的前序遍历和中序遍历,输出后序遍历 [思路] 根据前序遍历和中序遍历递归建树,再后续遍历输出 m ...

  6. HDU 1710 Binary Tree Traversals

    题意:给出一颗二叉树的前序遍历和中序遍历,输出其后续遍历 首先知道中序遍历是左子树根右子树递归遍历的,所以只要找到根节点,就能够拆分出左右子树 前序遍历是按照根左子树右子树递归遍历的,那么可以找出这颗 ...

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

                                                                                Binary Tree Traversals T ...

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

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  9. 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 disjo ...

随机推荐

  1. poj1860 & poj2240(Bellman-Ford)

    1860的思路是将可以换得的不同种的货币的数量当作节点,每个兑换点当成边,然后我抄了个算法导论里面的Bellman-Ford算法,一次就过了.看discussion里面很多讨论精度的,我想都没想过…… ...

  2. Python中__new__()方法的使用和实例化

    new()是在新式类中新出现的方法,它作用在构造方法init()建造实例之前,可以这么理解,在Python 中存在于类里面的构造方法init()负责将类的实例化,而在init()调用之前,new()决 ...

  3. BZOJ4530 BJOI 2014 大融合

    对LCT子树大小进行维护. size表示实子树大小,sz表示虚子树大小. 具体操作是体现在link和splay中,可以看代码. 注意每次做完都要update. By:大奕哥 #include<b ...

  4. [BZOJ4196][NOI2015]软件包管理器(树链剖分)

    4196: [Noi2015]软件包管理器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 2166  Solved: 1253[Submit][Sta ...

  5. 冒泡排序 Exercise07_18

    import java.util.Arrays; import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年12月 * 题目:冒泡排序 * */ pu ...

  6. bzoj 1834: [ZJOI2010]network 网络扩容 -- 最大流+费用流

    1834: [ZJOI2010]network 网络扩容 Time Limit: 3 Sec  Memory Limit: 64 MB Description 给定一张有向图,每条边都有一个容量C和一 ...

  7. bzoj4753 最佳团体

    题目描述 JSOI 信息学代表队一共有 NN 名候选人,这些候选人从 11 到 NN 编号.方便起见,JYY 的编号是 00 号.每个候选人都由一位编号比他小的候选人R_iRi​ 推荐.如果 R_i ...

  8. uva 6952 Cent Savings dp

    Cent Savings Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/vie ...

  9. Codeforces Round #302 (Div. 2) B. Sea and Islands 构造

    B. Sea and Islands Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/544/p ...

  10. Advanced Features of Delphi DLLs

    http://www.delphisources.ru/pages/faq/master-delphi-7/content/LiB0104.html Beside this introductory ...