Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

代码不用这么长吧

Posted by fantasyorg at 2008-03-21 23:29:44 on Problem 2255
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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator