We only know that the first sequence and the latter sequence cannot find the unique tree, so we will not discuss it.
[CPP]View plaincopy
- # Include <iostream>
- # Include <cstdio>
- # Include <cstring>
- Using namespace STD;
- Struct binarytreenode
- {
- Char C;
- Binarytreenode * lchild, * rchild;
- Binarytreenode ()
- {
- Lchild = NULL, rchild = NULL;
- }
- };
- Struct binarytreenode * root1, * root2;
- Char preorder [100], inorder [100], postorder [100];
- Void presearch (binarytreenode * root) // first traverse the tree
- {
- If (root! = NULL)
- {
- Printf ("% C", root-> C );
- Presearch (root-> lchild );
- Presearch (root-> rchild );
- }
- Return;
- }
- Void midsearch (binarytreenode * root) // The middle-order traversal tree.
- {
- If (root! = NULL)
- {
- Midsearch (root-> lchild );
- Printf ("% C", root-> C );
- Midsearch (root-> rchild );
- }
- Return;
- }
- Void postsearch (binarytreenode * root) // traverse the tree in descending order
- {
- If (root! = NULL)
- {
- Postsearch (root-> lchild );
- Postsearch (root-> rchild );
- Printf ("% C", root-> C );
- }
- Return;
- }
- Void buildtreefrompreandmid (binarytreenode * & root, int LL, int LR, int Len, Int & now) // calculate the tree in the middle and first order
- {
- Root = new binarytreenode ();
- Root-> C = * (preorder + now );
- Int Pos = (INT) (strchr (inorder, * (preorder + now)-inorder); // you can specify the position where a character first appears in a string.
- Now ++;
- If (now> = Len)
- Return;
- If (POS-1> = LL)
- {
- Binarytreenode * t = new binarytreenode ();
- Root-> lchild = T;
- Buildtreefrompreandmid (root-> lchild, ll, pos-1, Len, now );
- }
- If (Pos + 1 <= LR)
- {
- Binarytreenode * t = new binarytreenode ();
- Root-> rchild = T;
- Buildtreefrompreandmid (root-> rchild, POS + 1, LR, Len, now );
- }
- }
- Void buildtreefrompostandmid (binarytreenode * & root, int LL, int LR, int Len, Int & now) // calculate the tree in the middle and back order
- {
- Root = new binarytreenode ();
- Root-> C = * (postorder + now );
- Int Pos = (INT) (strchr (inorder, * (postorder + now)-inorder );
- Now --;
- If (now <0)
- Return;
- If (Pos + 1 <= LR)
- {
- Binarytreenode * t = new binarytreenode ();
- Root-> rchild = T;
- Buildtreefrompostandmid (root-> rchild, POS + 1, LR, Len, now );
- }
- If (POS-1> = LL)
- {
- Binarytreenode * t = new binarytreenode ();
- Root-> lchild = T;
- Buildtreefrompostandmid (root-> lchild, ll, pos-1, Len, now );
- }
- }
- // Release a binary tree
- Inline void deletebinarytree (binarytreenode * & root)
- {
- If (Root)
- {
- Deletebinarytree (root-> lchild); // release the left subtree
- Deletebinarytree (root-> rchild); // release the right subtree
- Delete root; // release the root node
- }
- }
- Int main (void)
- {
- Gets (preorder );
- Gets (inorder );
- // Gets (postorder );
- Int now = 0;
- Buildtreefrompreandmid (root1, 0, strlen (preorder)-1, strlen (preorder), now );
- // Int now2 = strlen (postorder)-1;
- // Buildtreefrompostandmid (root2, 0, strlen (postorder)-1, strlen (postorder), now2 );
- Postsearch (root1 );
- Puts ("");
- Deletebinarytree (root1 );
- /* Presearch (root2 );
- Puts ("");
- Deletebinarytree (root2 );*/
- Return 0;
- }