HDU--1710--Binary Tree Traversals(二叉树遍历)
来源:互联网 发布:php namespace use 编辑:程序博客网 时间:2024/04/30 10:43
Description
A binary tree isa finite set of vertices that is either empty or consists of a root r and twodisjoint binary trees called the left and right subtrees. There are three mostimportant ways in which the vertices of a binary tree can be systematicallytraversed or ordered. They are preorder, inorder and postorder. Let T be abinary tree with root r and subtrees T1,T2.
In a preorder traversal of the vertices of T, we visit the root r followed byvisiting 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 ininorder, 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 inpostorder, then the vertices of T2 in postorder and finally we visit r.
Now you are given the preorder sequence and inorder sequence of a certainbinary tree. Try to find out its postorder sequence.
Input
The inputcontains several test cases. The first line of each test case contains a singleinteger n (1<=n<=1000), the number of vertices of the binary tree.Followed by two lines, respectively indicating the preorder sequence andinorder sequence. You can assume they are always correspond to a exclusivebinary tree.
Output
For each testcase print a single line specifying the corresponding postorder sequence.
Sample Input
9
1 2 4 7 3 5 8 96
4 7 2 1 8 5 9 36
Sample Output
7 4 2 8 9 5 6 3 1
题意:给你一个前序遍历和中序遍历,要求后序。
模板.......
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 1005using namespace std;int pre[N],in[N],post[N]; //存放先序,中序,后序的数组 int n;struct Node{int data;Node* lchild;Node* rchild;};Node* create(int prel,int prer,int inl,int inr) //根据先序和中序建立树 { //4个参数 先序的左右边界,中序的左右边界 if(prel>prer) //已经遍历完了,返回 {return NULL;}Node* root=new Node; //建立一个根结点,用来存放当前的树 root->data=pre[prel]; // 因为是已知先序,所以当前树的值,一定等于先序的最左边的点 int k; //记录当前根节点在中序的位置 for(k=inl;k<inr;k++) {if(in[k]==pre[prel]) //找到位置,跳出 break;}int numleft=k-inl; //当前树的左子树的大小 root->lchild=create(prel+1,prel+numleft,inl,k-1); //将这部分存入左子树 root->rchild=create(prel+numleft+1,prer,k+1,inr); // 将这部分存入右子树return root; //返回根结点的地址 }int num=0; //控制最后一个输出没有空格 void printfpost(Node* root) //后序输出 {if(root==NULL)return;printfpost(root->lchild);printfpost(root->rchild);printf("%d",root->data);num++;if(num<n) printf(" ");}int main(){while(~scanf("%d",&n)){num=0;for(int i=0;i<n;i++){scanf("%d",&pre[i]);}for(int i=0;i<n;i++){scanf("%d",&in[i]);}Node* root=create(0,n-1,0,n-1);printfpost(root);printf("\n");}}
- HDU---1710-Binary Tree Traversals (二叉树遍历)
- HDU 1710 Binary Tree Traversals (二叉树遍历)
- HDU--1710--Binary Tree Traversals(二叉树遍历)
- hdu 1710 binary tree traversals 二叉树遍历
- 二叉树 遍历 hdu-1710-Binary Tree Traversals
- HDU-1710 Binary Tree Traversals 二叉树遍历
- hdu 1710 Binary Tree Traversals 二叉树的遍历
- HDU 1710(Binary Tree Traversals)二叉树的遍历
- (遍历二叉树)HDU 1710 Binary Tree Traversals
- HDU 1710 Binary Tree Traversals 二叉树
- HDU 1710 Binary Tree Traversals(二叉树)
- HDU 1710 Binary Tree Traversals(二叉树的后序遍历)
- HDU 1710 Binary Tree Traversals(二叉树)
- HDU 1710-Binary Tree Traversals(重建二叉树)
- 【二叉树之遍历的应用】HDU 1710---Binary Tree Traversals
- HDOJ 1710Binary Tree Traversals 二叉树的遍历
- hdu1710 Binary Tree Traversals(二叉树遍历)
- HDU 1701 Binary Tree Traversals 【二叉树】
- Java---学习(5)
- 1009. 说反话 (20)
- Mongo数据格式及数据类型
- opencv环境配置
- 【刷题日记】向NOIP2017冲刺!
- HDU--1710--Binary Tree Traversals(二叉树遍历)
- Java---学习(6)
- 51nod 1086 背包问题V2 (巧妙dp,二进制)
- 538. Convert BST to Greater Tree
- 卡尔曼滤波
- Java---学习(7)
- python中hashlib模块
- 链家、CVTE笔试题
- 简单的分布式进程