HDU-1710-Binary Tree Traversals

Source: Internet
Author: User

 

Binary Tree Traversals

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 2468 Accepted Submission (s): 1072

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

 

Code:

Search for post-order traversal from pre-order traversal and end-to-end traversal and recursion to distinguish Sequences

 

# Include "stdio. h "# include" string. h "int s1 = 1, s3 = 1; // sequence numbers of the forward and backward orders: int preseq [1010], inseq [1010], postseq [1010]; void find (int a, int B) // indicates that it is from ~ A tree of B {int I; if (a> B) return; // The tree has no element if (a = B) // there is only one element {// directly store this element into postseq, s1 ++, and judge the next element of preseq: postseq [s3 ++] = inseq [a]; s1 ++; return;} // printf ("**"); preseq [0] = preseq [s1]; // find the position of the root in inpost for (I =; inseq [I]! = Preseq [0]; I ++); s1 ++; find (a, I-1); // left subtree find (I + 1, B ); // right subtree postseq [s3 ++] = inseq [I]; // root} int main () {int I, n; while (scanf ("% d ", & n )! = EOF) {for (I = 1; I <= n; I ++) scanf ("% d", & preseq [I]); for (I = 1; I <= n; I ++) scanf ("% d", & inseq [I]); find (1, n); for (I = 1; I <n; I ++) // outputs printf ("% d", postseq [I]); printf ("% d \ n", postseq [n]); s1 = 1; s3 = 1; // The serial number is changed back to 1} return 0;}/* 9 1 2 2 4 7 3 5 9 6 4 2 1 5 9 3 6 */

 

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.