The third kind of traversal result is obtained from the two kinds of traversal in the sequence traversal, middle order traversal and post order traversal.

Source: Internet
Author: User
Tags data structures
Learn about data structures, we all know that there are four ways to traverse a binary tree, the sequence traversal, the middle sequence traversal, the post-order traversal and the sequence traversal, and the first three kinds of traversal have strong correlation, namely: knowing the middle sequence traverse and one of the other two kinds of traversal, we can find the third kind, simply speaking is according to the middle sequence traverse and the preface , you can ask for a third kind.
is not a bit around, I slowly understand it. Let's talk about implementation code here.
meeting This problem, I heard it seems to be possible to use the stack to achieve, but today is to say through the achievements to achieve. There
are two cases:
1, know the pre-sequence traversal and the middle sequence traversal, to seek post-order traversal.
                This is relatively simple, because we can find the root node in the middle sequence traverse the location of the two traversal sequences are cut, divided into sub-sequences, so that through recursion can achieve achievements, and the recursive function at the end of an output statement can be implemented after the post-order traversal.
2, know the post-order traversal and the middle sequence traversal, to seek the pre-sequence traversal.
                This is a bit difficult, to three kinds of traversal does not teach in-depth understanding of the algorithm, in fact, and the front of the algorithm roughly the same, just a little detail difference, we are also to find the root node in the middle sequence traversal position, but we can not be separated by the same means, we know, the middle sequence traversal, The left side of the root node is the left subtree, on the right are the right sub-tree, and then the root node in the end of the sequence traversal, so want to separate must be in the middle sequence traversal to the root node as the center of the split (leaving no root node), the segmentation of the post-order traversal to ensure that the first half of the length and the first half of the sequence traversal length consistent, The second half of the section to remove the root node, so divided into two segments is also the left and right sub-tree, and the output statement also to advance, because this is a pre-order traversal, so the output statement needs to be in the recursive function before the output of the call itself.

The general idea is this, first look at the code (code C + +):

#include <iostream> #include <fstream> #include <string> struct TreeNode {struct treenode* left;
   struct treenode* right;
Char Elem;

}; void Binarytreefromorderings (char* inorder, char* preorder, int length) {if (length = = 0) {//cout<< "
       Invalid Length ";
    Return
    } treenode* node = new Treenode;//noice that [new] should is written out.
    Node->elem = *preorder;
    int rootindex = 0;
    for (; rootindex < length; rootindex++) {if (inorder[rootindex] = = *preorder) break;
    }//left binarytreefromorderings (inorder, preorder +1, rootindex);
    Right binarytreefromorderings (inorder + rootindex + 1, preorder + rootindex + 1, Length-(rootindex + 1));
    cout<<node->elem<<endl;
Return
    } int main (int argc, char* argv[]) {printf ("Hello world!\n");
    char* pr= "Gdafemhz";

    char* in= "ADEFGHMZ";

    Binarytreefromorderings (in, PR, 8);
 printf ("\ n");   return 0; }

Next, the code for the second case (code C):

#include <stdio.h> #include <string.h> #include <stdlib.h> typedef char Telemtype; #define MAX_TREE_SIZE-typedef struct BITNODE//node structure {telemtype data;//Node database struct Bitnode *lchild, *rchild    ;

Left and right child pointer} bitnode, *bitree; Build and pre-sequence traversal void binarytreefromorderings (char *mid, char *last, int len) {///length 0 returns empty if (len = = 0) {RE
    turn;  } bitree node = (bitree) malloc (sizeof (Bitnode));
    Allocation node Space node->data = last[len-1];
    Looking for the root node int rootindex = 0 in mid;
        for (; Rootindex < Len; rootindex++) {if (mid[rootindex] = = Last[len-1]) {break;
    }}//Pre-order traversal, then a node is built to print once printf ("%c", node->data);
    Left Binarytreefromorderings (Mid, Last, Rootindex);  Right binarytreefromorderings (mid + Rootindex + 1, last + Rootindex, Len-(Rootindex + 1));
The latter segment is the right subtree return;
    } int main (int argc, const char * argv[]) {char mid[100], last[100], Len; ScanF ("%s%s", Mid, last);

    len = (int) strlen (mid);

    Binarytreefromorderings (Mid, Last, Len);
    printf ("\ n");
return 0;
 }

The two conditions of the algorithm are the same, the difference lies in the location of the sequence cutting different, cutting way is different, that is, there are some problems in the details need to pay more attention to, the other is nothing, on, to learn how to use the pointer, there is no perfect pointer to use the foundation, will be very bad to do, After all, it is the knowledge of the data structure.

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.