Rebuilding a binary tree

Source: Internet
Author: User

Rebuilding a binary tree
    • Number of participants: 1892 time limit: 1 seconds space limit: 32768K
    • By scale: 19.08%
    • best record: 0 ms|0k (from Shi_kai)
The topic describes the input of a binary tree's pre-sequence traversal and the results of the middle sequence traversal, please reconstruct the two-fork tree. Assume that no duplicate numbers are included in the result of the input's pre-order traversal and the middle-order traversal. For example, enter the pre-sequence traversal sequence {1,2,4,7,3,5,6,8} and the middle sequence traversal sequence {4,7,2,1,5,3,8,6}, then rebuild the binary tree and return.

Binary tree, first review the characteristics of the following two fork tree:

about binary tree; n total =n0+n1+n2;n0=n2 + 1; SO...N Total =n1+2*n2+1

The maximum node count per layer 2^ (n-1); The maximum number of nodes is 2^n-1.

About traversal: The first order is: root around;

The middle order is: Zogen right;

The following is: left and right root;

In general, we will choose the recursive method to traverse the code back simple, this problem is the same.

But since the framework of the function has been given, the code is recycled to the limit;

Thinking or recursion, the method is segmented recursion;

/*struct TreeNode {int val;    TreeNode *left;    TreeNode *right; TreeNode (int x): Val (x), left (null), right (null) {}}; TreeNode *root;*/class Solution {public:struct treenode* reconstructbinarytree (vector<int> pre,vector<int        > in) {//To determine the recursive termination condition; if (pre.size () = = 0 | | in.size () = = 0) {return NULL;        }//define node nodes and seek root node; int root = pre[0];        treenode* node = new TreeNode (root);        Vector<int>::iterator it;        The ergodic sequence of the left and right sub-trees was obtained; Vector<int> Preleft, Preright, Inleft, inright;        1. Finding the position of the root node in the sequential traversal sequence; Vector<int>::iterator i;            for (it = In.begin (); It! = In.end (); it++) {if (root = *it) {i = it;        }}//2, to find the left and right sub-tree of the middle sequence traversal sub-sequence; int k = 0;            for (it = In.begin (); It! = In.end (); it++) {if (k = = 0) inleft.push_back (*it);            else if (k = = 1) inright.push_back (*it); if (it = = i) k = 1;        }//3, to find the left and right sub-tree of the pre-sequence traversal sub-sequence; k = 0;        Vector<int>::iterator ite;            for (it = Pre.begin () +1; it! = Pre.end (); it++) {for (ite = Inleft.begin (); ITE! = Inleft.end (); ite++)                    {if (*it = = *ite) {preleft.push_back (*it);                K = 1;            }} if (k = = 0) {preright.push_back (*it);        } k = 0;        }//The left and right nodes are calculated according to the ergodic sequence; node->left = Reconstructbinarytree (preleft,inleft);        Node->right = Reconstructbinarytree (preright,inright);    return node address; }};


Learned the method can be done on the hduoj, practice a bit;

Test instructions is: pre-order and sequential output binary tree is known!

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1710

249ms AC

#include <stdio.h> #include <vector>using namespace std;struct TreeNode {int val;    TreeNode *left;    TreeNode *right; TreeNode (int x): Val (x), left (null), right (null) {}}; TreeNode *root;class Solution {public:struct treenode* reconstructbinarytree (vector<int> pre,vector<int>        IN) {//To determine the recursive termination condition; if (pre.size () = = 0 | | in.size () = = 0) {return NULL;        }//define node nodes and seek root node; int root = pre[0];        treenode* node = new TreeNode (root);        Vector<int>::iterator it;        The ergodic sequence of the left and right sub-trees was obtained; Vector<int> Preleft, Preright, Inleft, inright;        1. Finding the position of the root node in the sequential traversal sequence; Vector<int>::iterator i;            for (it = In.begin (); It! = In.end (); it++) {if (root = *it) {i = it;        }}//2, to find the left and right sub-tree of the middle sequence traversal sub-sequence; int k = 0;            for (it = In.begin (); It! = In.end (); it++) {if (k = = 0) inleft.push_back (*it); else if (k = = 1) inright.push_back (*it);        if (it = = i) k = 1;        }//3, to find the left and right sub-tree of the pre-sequence traversal sub-sequence; k = 0;        Vector<int>::iterator ite;            for (it = Pre.begin () +1; it! = Pre.end (); it++) {for (ite = Inleft.begin (); ITE! = Inleft.end (); ite++)                    {if (*it = = *ite) {preleft.push_back (*it);                K = 1;            }} if (k = = 0) {preright.push_back (*it);        } k = 0;        }//The left and right nodes are calculated according to the ergodic sequence; node->left = Reconstructbinarytree (preleft,inleft);        Node->right = Reconstructbinarytree (preright,inright);    return node address;    }};/*** Post-Traversal * * * * left and right root ***/void posorderrecur (TreeNode *head) {if (head==null) return;    Posorderrecur (Head->left);    Posorderrecur (Head->right);    if (head==root) printf ("%d\n", head->val); else printF ("%d", head->val);}    int main () {int n;    Solution so;        while (scanf ("%d", &n) >0) {root=new TreeNode (NULL);        Vector<int> A, B;        int x;            for (int i=0;i<n;i++) {scanf ("%d", &x);        A.push_back (x);            } for (int i=0;i<n;i++) {scanf ("%d", &x);        B.push_back (x);        } root=so.reconstructbinarytree (A, b);        TreeNode *h=root;    Posorderrecur (h); } return 0;}


Then in providing an array recursion, the use of pointers will be much easier.

46ms AC

#include <stdio.h> #include <vector>using namespace std;struct TreeNode {int val;    TreeNode *left;    TreeNode *right; TreeNode (int x): Val (x), left (null), right (null) {}}; TreeNode *root;    TreeNode *creat (int *pre,int *in,int n) {TreeNode *s;            for (int i=0;i<n;i++) {if (Pre[0]==in[i]) {s=new TreeNode (in[i]);            The S->left=creat (pre+1,in,i) on the left side of the root node in the middle sequence of the calendar passes;            On the right side of the root node are the right subtree, and the right subtree needs to start from i+1 s->right=creat (pre+i+1,in+i+1,n-i-1);        return s; }} return NULL;}    /*** Post-Traversal * * * * left and right root ***/void posorderrecur (TreeNode *head) {if (head==null) return;    Posorderrecur (Head->left);    Posorderrecur (Head->right);    if (head==root) printf ("%d\n", head->val); else printf ("%d", head->val);}    int main () {int n;        while (scanf ("%d", &n)!=eof) {root=null;        int a[2005],b[2005];  for (int i=0;i<n;i++) scanf ("%d", &a[i]);      for (int i=0;i<n;i++) scanf ("%d", &b[i]);        Root=creat (A,b,n);        TreeNode *head=root;    Posorderrecur (head); } return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Rebuilding a binary tree

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.