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
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.
#include<iostream>
#include<cstring>
#include<cstdio>
#include<stdlib.h>
using namespace std;
int m;
typedef struct tree
{
tree *l,*r;
int num;
}tree;
tree *creat(int *a,int *b,int n)
{
tree *t;
int i;
for(i=;i<n;i++)
{
if(a[]==b[i])//找到中序遍历时的根节点
{
t=(tree*)malloc(sizeof(tree));
t->num=b[i];
t->l=creat(a+,b,i);//中序历遍中在根节点左边的都是左子树上的
t->r=creat(a+i+,b+i+,n-i-);//在根节点右边的,都是右子树上的,右子树需要从i+1开始
return t;
}
}
return NULL;
}
void postorder(tree *root)//后序遍历。
{
if(root!=NULL)
{
postorder(root->l);
postorder(root->r);
if(m==root->num)
printf("%d\n",root->num);
else
printf("%d ",root->num);
}
} int main()
{
int i,n;
int a[],b[];
while(~scanf("%d",&n))
{
tree *root;
for(i=;i<n;i++)
scanf("%d",&a[i]);
for(i=;i<n;i++)
scanf("%d",&b[i]);
root=creat(a,b,n);
m=a[];
// printf("root=%d\n",root->num);
postorder(root); }
return ;
}
HDU-1701 Binary Tree Traversals的更多相关文章
- hdu 1701 (Binary Tree Traversals)(二叉树前序中序推后序)
Binary Tree Traversals T ...
- HDU 1710 Binary Tree Traversals (二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 1710 Binary Tree Traversals(树的建立,前序中序后序)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- hdu 1710 Binary Tree Traversals 前序遍历和中序推后序
题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (J ...
- HDU 1710 Binary Tree Traversals(二叉树遍历)
传送门 Description A binary tree is a finite set of vertices that is either empty or consists of a root ...
- HDU 1710 Binary Tree Traversals(二叉树)
题目地址:HDU 1710 已知二叉树先序和中序求后序. #include <stdio.h> #include <string.h> int a[1001], cnt; ty ...
- 【二叉树】hdu 1710 Binary Tree Traversals
acm.hdu.edu.cn/showproblem.php?pid=1710 [题意] 给定一棵二叉树的前序遍历和中序遍历,输出后序遍历 [思路] 根据前序遍历和中序遍历递归建树,再后续遍历输出 m ...
- HDU 1710 Binary Tree Traversals
题意:给出一颗二叉树的前序遍历和中序遍历,输出其后续遍历 首先知道中序遍历是左子树根右子树递归遍历的,所以只要找到根节点,就能够拆分出左右子树 前序遍历是按照根左子树右子树递归遍历的,那么可以找出这颗 ...
- HDU 1710 二叉树的遍历 Binary Tree Traversals
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- hdu1710(Binary Tree Traversals)(二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
随机推荐
- cordova安装中的坑
1.安装android环境直接略过! 2.安装node.js直接略过! 3.安装cordova npm install -g cordova npm uninstall cordova -g(这条是 ...
- 如何讓Android系統顯示CJK擴展區漢字
由於一些特殊需要,需要在個人設備上顯示CJK擴展區漢字,經多方詢問並驗證,找到了一下辦法,暫總結如下. 一.電腦上顯示 在電腦(Windows,Linux,Mac等系統)上可以通過安裝「花園明朝字體」 ...
- linux管道学习(二)
int main() { char* pipename = "pipe"; mkfifo(pipename,); int pid = fork(); ) { printf(&quo ...
- Linux网络应用编程之Packet Tracer安装及界面介绍
Packet Tracer入门 一,Packet Tracer介绍 packet tracer 是由Cisco公司发布的一个辅助学习工具,为学习思科网络课程的初学者去设计.配置.排除网络故障提供了网络 ...
- HTML 表单和表格
1.使用表单标签 网站使用 HTML 表单可与用户进行交互,表单元素是允许用户在表单中输入内容,比如:文本框.文本域.单选框.复选框.下拉列表.按钮等等,表单可以把浏览者输入的数据传送到服务器端,这样 ...
- 经典的C程序
程序一:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数 #include<stdio.h> void main(){ int a, b, c, i; ; ...
- (转载)delphi实例TDBGrid用右键菜单复制行粘贴行
delphi实例TDBGrid用右键菜单复制行粘贴行 这个从本质上来说就是DBGrid后台数据库的插入 右键复制当前行的相关数据到临时变量点粘贴时,覆盖数据或插入数据! db为数据库: 字段名id,n ...
- Listview 加载更多
JQM Listview 加载更多 demo - Warren的个人主页 JQM Listview 加载更多 Demo 测试数据1 测试数据2 测试数据3 测试数据4 显示更多 Page Footer ...
- 【资料目录收藏】.NET开发必看资料53个 经典源码77个
简单描述:为大家整理了下载中心.net资料,都是比较热的,好评率比较高的,做了一个可收藏的下载目录,希望大家喜欢~ 基于.net构架的留言板项目大全源码 http://down.51cto.com/z ...
- MyEclipse配置多个WEB容器
MyEclipse支持多个同版本WEB容器同时运行 打开 然后按下图操作 咱们就得到了 下面需要配置新增加WEB容器的启动路径,在新增加的WEB容器上点击右键,选择箭头指向的菜单 打开的窗口如图,可以 ...