zoukankan      html  css  js  c++  java
  • 由一棵二叉树的先序序列和中序序列可唯一确定这棵二叉树

    思路:

    (1)由先序可以得到树的根节点。

    (2)由中序可以得到左右子树。

    (3)重复(1)(2)即可恢复

    同理给出后续和中序也可以按照上述思想唯一确定一棵树(这个程序转的http://www.cnblogs.com/microgrape/archive/2011/05/12/2043932.html

     1 #include <iostream>
     2 #include <map>
     3 #include <utility>
     4 #include <functional>
     5 #include <string>
     6 #include <stack>
     7 
     8 using namespace std;
     9 
    10 typedef char Type;
    11 
    12 struct Node
    13 {
    14      Node( Node *t ) : data( t->data ), lChild( t->lChild ), rChild( t->rChild ) {}
    15      Node( Type d ) : data( d ), lChild( NULL ), rChild( NULL ) {}
    16 
    17     struct Node* lChild;
    18     struct Node* rChild;
    19      Type data;
    20 };
    21 
    22 Node *rebuildFromPreInOrder( string &preOrder, string &inOrder )
    23 {
    24     // preOrder and inOrder always have equal length.
    25     if ( preOrder.size() == 0 )
    26         return NULL;
    27     if ( preOrder.size() == 1 ) 
    28         return new Node( preOrder[0] );
    29 
    30     // get the root
    31      Node *root = new Node( preOrder[0] );
    32 
    33     // divide inOrder sequence into two sub sequences 
    34     // by the first node of preOrder sequence.
    35      size_t rootPos = inOrder.find( preOrder[0] );
    36     string left_sub_inorder = inOrder.substr( 0, rootPos );
    37     string right_sub_inorder = inOrder.substr( rootPos + 1 );
    38      size_t left_size = left_sub_inorder.size();
    39     
    40     // divide preOrder sequence into two sub sequences by their length.
    41     string left_sub_preorder = preOrder.substr( 1, left_size );
    42     string right_sub_preorder = preOrder.substr( left_size + 1 );
    43 
    44     // recursive rebuilding and connect with root
    45      root->lChild = rebuildFromPreInOrder( left_sub_preorder, left_sub_inorder );
    46      root->rChild = rebuildFromPreInOrder( right_sub_preorder, right_sub_inorder );
    47     return root;
    48 }
    49 
    50 void PostOrder( Node *root, void (*visit)(Node*) )
    51 {
    52      stack<Node*> s;
    53      Node *cur = root;
    54      Node *visited = NULL;
    55 
    56     while( ! s.empty() || cur != NULL )
    57      {
    58         while( cur != NULL )
    59          {     s.push( cur ); cur = cur->lChild; }
    60 
    61          cur = s.top();  // check but no visit.
    62 
    63         if( cur->rChild == visited || cur->rChild == NULL )
    64          {
    65              visit( cur ); s.pop();
    66              visited = cur;
    67              cur = NULL; // no current node, must pop.
    68          }
    69         else
    70              cur = cur->rChild;
    71      }
    72 }
    73 
    74 void visit( Node *t )
    75 {
    76      cout<<t->data;
    77 }
    78 
    79 int main()
    80 {
    81     while ( !cin.eof() )
    82      {
    83         string pre, in;
    84          cin>>pre>>in;
    85          Node *root = rebuildFromPreInOrder( pre, in );
    86          PostOrder( root, visit );
    87          cout<<endl;
    88      }
    89     
    90     return 0;
    91 }

     

  • 相关阅读:
    codeforces 505E Mr. Kitayuta vs. Bamboos 题解
    codeforces 568C New Language 题解
    [AGC020E] Encoding Subsets 题解
    技巧瞎扯
    [AGC028C] Min Cost Cycle 题解
    [AGC018D] Tree and Hamilton Path 题解
    codeforces 1217D Coloring Edges 题解
    [AGC003C] BBuBBBlesort! 题解
    [AGC037C] Numbers on a Circle 题解
    [USACO09Open] Tower of Hay 题解
  • 原文地址:https://www.cnblogs.com/GoAhead/p/2515069.html
Copyright © 2011-2022 走看看