SJTU-1040-binary tree hierarchy Traversal

Source: Internet
Author: User

 

1040. Binary Tree hierarchy Traversal

Description

A binary tree is given and its layered traversal result is obtained.

[Binary tree traversal is a kind of spirit and must be understood]

Input Format

The first line, N <1000000, indicates the number of Binary Tree nodes.

The default node number 0 is the root of the node. Next a total of N-1 lines, in turn indicates the number of 1,..., N-1 node Father's Day node number.

If a node has two child nodes, the serial number of the left child node is always smaller than that of the right child node.

Output Format

Only one row, the result of layered traversal of a binary tree. Node numbers are separated by spaces.

HintSample Input
601104
Sample Output
0 1 4 2 3 5

 

 

 

Code:

 

First time-out

Because an extremely large array is directly allocated and the data in the array is searched one by one, the time complexity caused by the number of cycles is so large that the result timeout is required.

 

# Include "stdio. h "// timeout code. The following is the correct AC # include" stdlib. h "# include" queue "# include" iostream "using namespace std; int tree [1000010]; int print [1000010]; int n; // n knots <int> out; void Traverse (int tree []) {int I = 0, j, num = 0, pi = 1, e; out. push (I); // 0 for (j = 1; j <= n; j ++) {num = 0; e = out. front (); // leaves an element and searches for the node out with this element as the parent node. pop (); // printf ("e: % d \ n", e); for (I = 1; I <n; I ++) // starts the loop from 0 subscript, the parent node is I, and the output is {if (num = 2) break; // two Subtrees If (tree [I] = e) {// printf ("---- \ n"); out. push (I); // printf ("back: % d \ n", out. back (); print [pi ++] = I; // printf ("print: % d \ n", print [pi-1]); num ++ ;}}} int main () {int I; while (scanf ("% d", & n )! = EOF) {for (I = 1; I <n; I ++) scanf ("% d", & tree [I]); // store the parent node with an array, subscript indicates the node element // for (I = 0; I <n; I ++) // print [I] = 0; print [0] = 0; traverse (tree); for (I = 0; I <n-1; I ++) // outputs printf ("% d", print [I]); printf ("% d \ n", print [n-1]);} return 0 ;}

The second answer is incorrect.

The operator has written the wrong array subscript and has spent half a day

 

The third time I finally passed, burst into tears.

 

# Include "stdio. h "# include" stdlib. h "# include" queue "# include" iostream "using namespace std; int tree [1000010] [2]; int print [1000010]; int n; // n knots <int> out; void Traverse (int tree [] [2]) {int e; e = 0; out. push (e); // enter 0 first while (n --) // n cycles {if (tree [e] [0]! =-1) out. push (tree [e] [0]); // if (tree [e] [1] if not-1! =-1) out. push (tree [e] [1]); printf ("% d", out. front (); // if (n> 0) printf (""); out. pop (); // The output Element e = out. front (); // e is assigned to the current head element} printf ("\ n");} int main () {int I, temp; while (scanf ("% d", & n )! = EOF) {for (I = 0; I <n; I ++) // because there may only be two Subtrees, if the input father node is the same, tree [I] [0] = tree [I] [1] =-1; // put it in these two locations to save time for (I = 1; I <n; I ++) // store the parent node in an array. The subscript represents the node element {scanf ("% d", & temp ); if (tree [temp] [0] =-1) {tree [temp] [0] = I ;} else // This Father's Day node already has a child node {if (temp> tree [temp] [0]) // compare the size with the existing node, place a smaller summary point at 0: {tree [temp] [1] = tree [temp] [0]; tree [temp] [0] = I ;} tree [temp] [1] = I ;}// enter for // for (I = 0; I <n; I ++) // printf ("% d: % d \ n ", I, tree [I] [0], tree [I] [1]); Traverse (tree);} return 0 ;}

 

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.