HDU-1701 Binary Tree traversals

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1710

The first and middle order traversal are known, and the second order traversal binary tree is obtained.

Idea: first build the process of recursion, then traverse in order.

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


Problem descriptiona 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.
 

 

Inputthe 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.

 

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

 

Sample input9 1 2 4 7 3 5 8 9 6 4 2 1 8 5 9 3 6

 

Sample output7 4 2 8 9 5 6 3 1

 

Sourcehdu 2007-spring Programming Contest
# 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 = 0; I <n; I ++) {if (a [0] = B [I]) // locate the root node {T = (TREE *) malloc (sizeof (tree) in the middle sequence; t-> num = B [I]; t-> L = creat (a + 1, B, I ); // In the central calendar, the left side of the root node is T-> r = creat (a + I + 1, B + I + 1, n-i-1 ); // on the right of the root node, all are on the right subtree. The right subtree needs to return t from I + 1 ;}} Return NULL;} void postorder (TREE * root) // perform post-order traversal. {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 [2005], B [2005]; while (~ Scanf ("% d", & N) {tree * root; for (I = 0; I <n; I ++) scanf ("% d ", & A [I]); for (I = 0; I <n; I ++) scanf ("% d", & B [I]); root = creat (A, B, n); M = A [0]; // printf ("root = % d \ n", root-> num ); postorder (Root);} return 0 ;}

 

HDU-1701 Binary Tree traversals

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.