Constructing binary tree based on sequence + middle sequence traversal sequences

Source: Internet
Author: User

@ Algorithm Learning

The problem is: based on sequence traversal sequences + middle sequence traversal sequence, only one tree is established, then the pre-order and sequential traversal sequence are output.

The four ways of traversing the tree and the code naturally do not have to speak much, and it is interesting how to build the tree by sequence + middle order.

The first thing to say is that this is also a recursive solution.

http://paste.ubuntu.com/24084585/

Since it is a recursive solution, we need to think about the problem of the current layer , given a sequence of sequential traversal sequences + sequence traversal sequences , the current can determine the root node is the first value of sequence sequences, Take this to find the root node of the subscript k in the middle order, and then divide out the left and right sub-trees.
Now the problem is interesting, Zuozi in the sequence of sequences are not in a group, but scattered, in other words, Zuozi and right subtree of the sequence traversal sequences are crossed together, what to do.

We think it would be nice to have Zuozi sequence traversal sequences and sequence traversal sequences of right subtrees.

This is actually a very beautiful view, based on this can solve the problem.

Open two arrays are dedicated to the traversal sequence of the left and right sub-trees .

One of the versions I rewrote:

#include <stdio.h> #include <vector> #include <queue> using namespace std;

const int MAXN = 40;
int IN[MAXN];

Vector<int> layer; Vector<int> Pre,post;
    Save and then output is OK, but it takes a little time to struct node {int data;
    Node* left;
node* right;

};
    node* newNode (int val) {node* node = new node;
    Node->left = NULL;
    Node->right = NULL;
    Node->data = val;
return Node;
    } void Preorder (node* root) {if (!root) return;
    Pre.push_back (Root->data);
    Preorder (Root->left);
Preorder (root->right);
    } void Postorder (node* root) {if (!root) return;
    Postorder (Root->left);
    Postorder (Root->right);
Post.push_back (Root->data); } node* Createfromlevelinorder (vector<int> layer, int inL, int inR) {if (layer.size () = = 0)//If the sequence is exhausted, it represents a recursive boundary, which can be
    Previous regression {return NULL;
    }//handling problems with the current layer node* root = NewNode (layer[0]); int k; In the middle order, divide for (k = InL; k <= InR; k++) {if (LayEr[0] = = In[k]) {break;//Find out on the loop}} vector<int> Leftlayer; Sequence traversal sequences of left dial hand tree vector<int> rightlayer;
        Right subtree sequence traversal sequences for (int i = 1; i < layer.size (); i++)//traverse the current sequence, dividing left-to-save {bool Isleft = false;
                for (int j = InL; J < K; J + +) {if (layer[i] = = In[j]) {isleft = true; Break
        Make sure one jumps out and is currently in the outer layer of the Big Loop} if (Isleft) {leftlayer.push_back (layer[i]);
        } else {rightlayer.push_back (layer[i]);
    }}//In particular, it is necessary to use the left and right hands of root to connect the next layer of construction Root->left = Createfromlevelinorder (Leftlayer,inl, k-1);

    Root->right = Createfromlevelinorder (rightlayer,k + 1, InR);
return root;
    } int main () {int n;
    scanf ("%d", &n);
    int temp;
        control input for (int i = 0; i < n; i++) {scanf ("%d", &temp); Layer.push_back (temp); //input sequence Traversal sequences} for (int i = 0; i < n; i++) {scanf ("%d", &in[i]);//input middle sequence traversal sequence} node* root

    = Createfromlevelinorder (layer,0,n-1);
    Preorder (root);

    Postorder (root);
        Control output for (int i = 0; i < pre.size (); i++) {printf ("%d", pre[i]);
        if (I < pre.size ()-1) {printf ("");

    }} printf ("\ n");
        for (int i = 0; i < post.size (); i++) {printf ("%d", post[i]);
        if (I < post.size ()-1) {printf ("");
    }} 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.