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 ...
随机推荐
- O-c中类的继承与派生的概念
什么是继承 众所周知,面向对象的编程语言具有: 抽象性, 封装性, 继承性, 以及多态性 的特征. 那么什么是继承呢? 传统意义上是指从父辈那里获得父辈留下的东西 在开发中, 继承就是"复用 ...
- java新手笔记14 类继承示例
1.Person package com.yfs.javase; public class Person { private String name; private int age; private ...
- LigerUI API
参数列表 参数名 类型 描述 默认值 title String 表格标题 null width String|Int 宽度值,支持百分比 'auto' height String|Int 高度值,支持 ...
- Dell Remote Access Controller 添加和配置 DRAC/MC 用户
iDRAC设置 单击“Configuration”(配置)选项卡并选择“Users”(用户). 单击“Username”(用户名)列下的 [Available](可用)添加新用户,或单击“Userna ...
- Sdut 2165 Crack Mathmen(数论)(山东省ACM第二届省赛E 题)
Crack Mathmen TimeLimit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Since mathmen take security ...
- 递归查找某个目录下是否存在NOTICE文件
从Catalogs.txt文件中,读取待检查的目录列表.检查这些目录中,是否存在NOTICE文件,如果没有则检查它的父目录,直到cd ..到Repository目录. 如果cd ..到Reposito ...
- python相关博客
入门:http://www.pythontip.com/ Python之禅--大道至简 美胜于丑,显胜于隐,简胜于繁,繁胜于杂,平胜于迭,疏胜于密,读胜于写...名可名, 请常名 http://www ...
- Linux 内核学习的经典书籍及途径
from:http://www.zhihu.com/question/19606660 知乎 Linux 内核学习的经典书籍及途径?修改 修改 写补充说明 举报 添加评论 分享 • 邀请回答 ...
- 曾经的10道JAVA面试题
1.HashMap和Hashtable的区别. 都属于Map接口的类,实现了将惟一键映射到特定的值上.HashMap 类没有分类或者排序.它允许一个null 键和多个null 值.Hashtable ...
- 『奇葩问题集锦』Fedora ubuntu 下使用gulp 报错 Error: watch ENOSPC 解决方案
用gulp启动,错误如下 Error: watch ENOSPC at exports._errnoException (util.js:746:11) at FSWatcher.start (fs. ...