Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
代码不用这么长吧In Reply To:WHY PE!!提交了N次就是PE Posted by:heiheigo at 2005-05-23 19:31:01 #include<iostream> #include<cstring> using namespace std; char preorder[ 30 ], inorder[ 30 ]; class Node { public: Node( char ele, Node * le = NULL, Node * ri = NULL ) : element( ele ), left( le ), right ( ri ) { } char element; Node *left; Node *right; }; Node *root; void plantTree( Node * & t, int begin, int end, int & i ) { if ( begin <= end ) { char temp = preorder [ i ++ ]; t = new Node( temp ); int j; for ( j = begin; j <= end; j ++ ) if ( inorder [j] == temp ) { plantTree( t->left, begin, j - 1, i); plantTree( t->right, j + 1, end, i); } } } void printPostorder( Node * t ) { if ( t != NULL ) { printPostorder( t->left ); printPostorder( t->right ); cout << t->element; } } int main() { while ( cin >> preorder >> inorder ) { int len = strlen( inorder ); int temp = 0; plantTree( root, 0, len - 1, temp ); printPostorder( root ); cout << endl; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator