PAT L2-006. tree traversal, patl2-006

Source: Internet
Author: User

PAT L2-006. tree traversal, patl2-006
L2-006. tree traversal

Time Limit Memory limit Code length limit Question determination procedure Author
400 MS 65536 kB 8000 B Standard Chen Yue
Given the post-and Middle-order traversal of a binary tree, please output ITS sequence traversal. Here we assume that the key values are all positive integers that are not equal to each other.

Input Format:

The first line of the input is a positive integer N (<= 30), which is the number of nodes in the binary tree. The second row shows the post-order traversal sequence. The third row shows the sequential traversal sequence. Numbers are separated by spaces.

Output Format:

Output the sequence traversal of the tree in a row. Numbers are separated by one space. No extra space is allowed at the beginning and end of a line.

Input example:
72 3 1 5 7 6 41 2 3 4 5 6 7
Output example:
4 1 6 3 5 7 2

Ideas:

Based on the nature of the binary tree, you can know:

Based on this rule, you can find the root node in the middle-order traversal, and then use the position of the root node in the back-order traversal to get the subsequent traversal and length of the left and right Subtrees. Then, based on their respective lengths, the left and right Subtrees are decomposed from the middle-order traversal to obtain their respective middle-order traversal.

For example, in the example2 3 1 5 7 6 4The root node of is4And then traverse in the descending order1 2 3 4 5 6 7According to the root node4The position of the sub-tree is divided into the descending order of the Left subtree1 2 3The length of the Left subtree is 3, which can be traversed in the descending order of the right subtree56 7The length of the right subtree is 3. Based on the left and right subtree length and Rule 1, we can see that the central order traversal of the left and right subtree is2 3 1And5 7 6. And so on.

1 # include <iostream> 2 # include <queue> 3 using namespace std; 4 struct tree // indicates a tree, record the following-order traversal array and Long Path 5 {6 int * h, * z, length; // h points to the back-order traversal array, and z points to the middle-order traversal array, length indicates the length of 7 trees (int * h, int * z, int length) 8 {9 this-> h = h; 10 this-> z = z; 11 this-> length = length; 12} 13 tree () {}; 14}; 15 16 queue <tree> Queue; // 17 int l; 18 int H [40]; // Save the result 19 int Z [40]; // Save the result 20 21 void dis (tree T) 22 {23 tree w; 24 int I; 25 int t; 26 Queue. push (T); 27 while (! Queue. empty () 28 {29 w = Queue. front (); 30 Queue. pop (); 31 if (w. length> 0) 32 {33 t = w. h [w. length-1]; // the last part of the post-order traversal is the root node 34 cout <t; 35 int l = 0; 36 while (w. z [l]! = T) l ++; // get the length of the Left subtree 37 if (l> 0) // enter 38 Queue if the left subtree exists. push (tree (w. h, w. z, l); 39 if (w. length-l-1) // if the right subtree exists, the length of the Left subtree and the root node is 40 Queue. push (tree (w. h + l, w. z + l + 1, w. length-l-1); 41 if (! Queue. empty () cout <"; // if the team is not empty, the node will be output later, output a space 42} 43} 44} 45 46 int main () 47 {48 cin> l; 49 int I; 50 I = 0; 51 while (I <l) 52 {53 cin> H [I]; 54 I ++; 55} 56 I = 0; 57 while (I <l) 58 {59 cin> Z [I]; 60 I ++; 61} 62 dis (tree (H, Z, l); 63 return 0; 64} 65

 

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.