HDU 1710 Binary Tree traversals pre-sequence traversal and mid-order delayed sequence

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=1710 a chain of questions;

Binary Tree traversalsTime limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 4205 Accepted Submission (s): 1904


Problem Descriptiona binary tree is a finite set of vertices that is either empty or consists of a root r and a disjoint Binary trees called the left and right subtrees. There is three most important ways in which the vertices of a binary tree can be systematically traversed or ordered. They is preorder, inorder and Postorder. Let T is 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, the n the vertices of T2 in preorder.

In a inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root R, followed by the Ver Tices 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 postor Der and finally we visit R.

Now is given the preorder sequence and inorder sequence of a certain binary tree. Try to find the its postorder sequence.

Inputthe input contains several test cases. The first line of all test case contains a single integer n (1<=n<=1000), the number of vertices of the binary tree . followed by lines, respectively indicating the preorder sequence and inorder sequence. You can assume they is always correspond to a exclusive binary tree.

Outputfor each test case, print a single line specifying the corresponding postorder sequence.

Sample Input
91 2 4 7 3 5 8 9 64 7 2 1 8 5 9 3 6

Sample Output
7 4 2 8 9 5 6 3 1

Test instructions: Gives the order of the pre-order and middle order traversal to deduce the order of sequential traversal. Practice: Grasp two points, the pre-order is the root of the calendar, so the first sequence is the root. Then the root of the sequence is divided into two halves, left is the left subtree, the right is the right sub-tree. Then the recursion is OK.

#include <cstdio> #include <algorithm>using namespace std;struct tree{struct tree* l;struct tree* r;int val; t Ree () {l=null;r=null;}}; int Pre[1010],in[1010];int n;tree* root;void build (int num,int l,int r,tree *rt)//pre num in_l in_r{int flag=-1;while (f Lag==-1) {for (int i=l;i<=r;i++) {if (pre[num]==in[i]) flag=i;} if (flag==-1) num++;} if (flag-l>0) {rt->l= (tree*) malloc (sizeof); * (Rt->l) =tree (); build (num+1,l,flag-1,rt->l);} if (r-flag>0) {rt->r= (tree*) malloc (sizeof); * (Rt->r) =tree (); build (Num+1,flag+1,r,rt->r);} Rt->val=pre[num];} void post (tree* NW) {if (Nw->l!=null) post (nw->l), if (Nw->r!=null) post (Nw->r), if (nw==root) printf ("%d", Nw->val); elseprintf ("%d", nw->val);} void del (tree* NW) {if (nw->l!=null) del (nw->l), if (Nw->r!=null) del (NW-&GT;R); free (NW);} int main () {while (scanf ("%d", &n)!=eof) {for (Int. i=0;i<n;i++) scanf ("%d", &pre[i]); for (int i=0;i<n;i++) scanf ("%d", &in[i]); root= (tree*) malloc (sizeof); *root=treE (); build (0,0,n-1,root); Post (Root);p UTS ("");  del (root);  } return 0;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1710 Binary Tree traversals pre-sequence traversal and mid-order delayed sequence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.