HDU 1710 Binary Tree Traversals(二叉树)
Posted dyhaohaoxuexi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 1710 Binary Tree Traversals(二叉树)相关的知识,希望对你有一定的参考价值。
Binary Tree Traversals
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16273 Accepted Submission(s): 6823
Problem Description
A
binary tree is a finite set of vertices that is either empty or
consists of a root r and two disjoint binary trees called the left and
right subtrees. There are three most important ways in which the
vertices of a binary tree can be systematically traversed or ordered.
They are preorder, inorder and postorder. Let T be a binary tree with
root r and subtrees T1,T2.
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.
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.
Input
The
input contains several test cases. The first line of each test case
contains a single integer n (1<=n<=1000), the number of vertices
of the binary tree. Followed by two lines, respectively indicating the
preorder sequence and inorder sequence. You can assume they are always
correspond to a exclusive binary tree.
Output
For each test case print a single line specifying the corresponding postorder sequence.
Sample Input
9
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6
Sample Output
7 4 2 8 9 5 6 3 1
题目大意与分析
给出二叉树的先序遍历与中序遍历,求后序遍历
因为对于先序遍历来说,最先输出的是根节点,而对于中序遍历来说,根节点的左边都是左子树,根据这一特点,可以写出递归调用
#include<bits/stdc++.h> using namespace std; int a[1005],b[1005]; struct node { int val; node *l,*r; }; node *root; node * pre_and_in_to_post(int *a,int *b,int n) //a控制当前子树的先序遍历,b为中序,n为当前子树的长度 { node *now; for(int i=0;i<n;i++) { if(a[0]==b[i]) //找到当前的根节点 { now=(node *)malloc(sizeof(node)); now->val=b[i]; now->l=pre_and_in_to_post(a+1,b,i); //中序遍历的根节点左边的都在左子树上 now->r=pre_and_in_to_post(a+i+1,b+i+1,n-i-1); return now; } } return NULL; } void postorder(node *t) { if(t!=NULL) { postorder(t->l); postorder(t->r); if(t==root) cout<<t->val<<endl; else cout<<t->val<<‘ ‘; } } int main() { int n; while(cin>>n) { root=NULL; for(int i=0;i<n;i++) { cin>>a[i]; } for(int i=0;i<n;i++) { cin>>b[i]; } root=pre_and_in_to_post(a,b,n); node *h=root; postorder(h); } }
以上是关于HDU 1710 Binary Tree Traversals(二叉树)的主要内容,如果未能解决你的问题,请参考以下文章
HDU 1710 Binary Tree Traversals(二叉树)
二叉树hdu 1710 Binary Tree Traversals
hdu 1710 Binary Tree Traversals 前序遍历和中序推后序