Rebuilding a binary tree

Source: Internet
Author: User

Enter the result of the forward and middle traversal of a binary tree. Create a New Binary Tree. Assume that the input results do not contain repeated numbers. For example, input pre-order traversal sequences {1, 2, 4, 7, 3, 5, 6, 8} and Middle-order traversal sequences {4, 7, 2, 1, 5, 3, 8, 6 }, then, the binary tree is rebuilt and its post-sequential traversal sequence is output. Input: the input may contain multiple test examples. For each test case, the first input behavior is an integer n (1 <= n <= 1000), indicating the number of nodes in the binary tree. The second line of the input contains n integers (the range of each element a is (1 <= a <= 1000), which indicates the pre-order traversal sequence of the binary tree. The third line of the input contains n integers (where the range of each element a is (1 <= a <= 1000): indicates the ordinal traversal sequence of the binary tree. Output: corresponds to each test case, and outputs a line: if the pre-order and Middle-order traversal sequences given in the question can constitute a binary tree, n integers are output, represents the post-order traversal sequence of a binary tree. Each element is followed by spaces. If the pre-order and mid-order traversal sequences given in the question cannot constitute a binary tree, "No" is output ". Example input: 81 2 4 7 3 5 6 84 7 2 1 5 3 8 681 2 4 7 5 6 84 1 2 5 3 8 6 sample output: 7 4 2 5 8 6 3 1 No code AC: Thought: Use sub-governance to build a tree! [Cpp] # include <stdio. h> # include <stdlib. h> typedef struct tree {int id; struct tree * lc; struct tree * rc;} tree, * p_tree; p_tree root; int * pre, * mid; int creat_tree (int low, int high, p_tree * t, int m_low, int m_high) {int I, count = 0, f1, f2; int flag; if (high-low! = M_high-m_low) {return 0;} if (low> high) // detail {return 1;} (* t) = (p_tree) malloc (sizeof (tree )); (* t)-> id = pre [low]; (* t)-> lc = NULL; (* t)-> rc = NULL; // if (low = high) // details // {// return 1; //} flag = 0; for (I = m_low; I <= m_high; I ++) {if (mid [I] = pre [low]) {flag = 1; break;} else {count ++ ;}} if (flag) {f1 = creat_tree (low + 1, low + count, & (* t)-> lc), m_low, M_low + count-1); if (! F1) {return 0;} f2 = creat_tree (low + count + 1, high, & (* t)-> rc), m_low + count + 1, m_high ); return f2;} else {return 0;} void out_put (p_tree t) {if (t) {out_put (t-> lc); out_put (t-> rc ); printf ("% d", t-> id) ;}} int main () {int I, n; while (scanf ("% d", & n )! = EOF) {pre = (int *) malloc (sizeof (int) * n); mid = (int *) malloc (sizeof (int) * n ); for (I = 0; I <n; I ++) {scanf ("% d", & pre [I]) ;}for (I = 0; I <n; I ++) {scanf ("% d", & mid [I]);} www.2cto.com if (creat_tree (0, n-1, & root, 0, n-1) {out_put (root);} else {printf ("No");} printf ("\ n");} 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.