题目地址: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. Bzoj2038/洛谷P1494 小Z的袜子(莫队)

    题面 Bzoj 洛谷 题解 考虑莫队算法,首先对询问进行分块(分块大小为\(sqrt(n)\)),对于同一个块内的询问,按照左端点为第一关键字,右端点为第二关键字排序.我们统计这个区间内相同的颜色有多 ...

  2. 关于python安全性的问题

    收集总结了一下python安全方面的知识点以及近年来的相关漏洞,如果有需要修正或补充的地方,欢迎各位师傅的指出. 常见web漏洞在python中的示例. xss python下的xss其原理跟php是 ...

  3. PHP代码重用

    代码重用 include() 和require() 都是载入文件 include()如果载入的文件不存在,提示警告错误,程序还可以继续执行 require()如果载入的文件不存在,致命性错误,程序终止 ...

  4. Codeforces 493 E.Devu and Birthday Celebration

    \(>Codeforces \space 493\ E.Devu\ and\ Birthday\ Celebration<\) 题目大意 : 有 \(q\) 组询问,每次有 \(n\) 小 ...

  5. DEDECMS最新友情链接getshell漏洞分析

    先上exp: <?php //print_r($_SERVER); $referer = $_SERVER['HTTP_REFERER']; $dede_login = str_replace( ...

  6. 扩展gcd codevs 1213 解的个数

    codevs 1213 解的个数  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 已知整数x,y满足如下面的条件: ax+by ...

  7. [转]Android:异步处理之AsyncTask的应用(二)

    2014-11-07     既然UI老人家都这么忙了,我们这些开发者肯定不能不识趣的去添乱阻塞UI线程什么的,否则UI界面万一停止响应了呢——这不是招骂的节奏么?!所以我们知道用Handler+Th ...

  8. 稀疏编码直方图----一种超越HOG的轮廓特征

    该论文是一篇来自CMU 的CVPR2013文章,提出了一种基于稀疏编码的轮廓特征,简称HSC(Histogram of Sparse Code),并在目标检测中全面超越了HOG(Histogram o ...

  9. Android 基于蓝牙的方向控制器

    最近开发了一个蓝牙控制器App,用手机远程控制小球的运动. 包含了一些基础知识:多线程使用,页面UI数据更新,按钮事件,选择项功能,蓝牙功能(蓝牙打开,蓝牙搜索,蓝牙连接,蓝牙命令发送,蓝牙命令接收) ...

  10. StringFormat

    public class StringFormatDemo { public static void main(String[] args) { String str = null; str = St ...