2 567432 543267 576342 0
思路:给出两个序列,以二叉搜索树的方式建树(建树方法见博客中的其它文章),然后判断两棵二叉搜索树是否相同,相同则输出YES,或者输出NO.
一开始的思路是,先建立两棵树,然后求出它们的先序和中序,比较相应的序列是否相同,则可以判断,但是这种办法是超时的,原因也比较明显,因为要多次递归。
因此,换成另一种思路,直接写一个判断两棵子树是否相同的子程序即可。
/* First time,I used inorde and preorder to judge if it's Yes or No,but Time Limit Exceeded, Second time,I change to isEqual(),then ,it's done. */ #include <iostream> #include <string> #include <limits.h> using namespace std; struct node { char key; node* left; node* right; node(char k):key(k),left(NULL),right(NULL){} }; void insert(node** root,node* z) { node *y = NULL, *x = *root; while(x) { y = x; if( y->key < z->key ) x = x->right; else x = x->left; } if ( y == NULL ) *root = z; else if (y->key < z->key ) y->right =z; else y->left = z; } void Del(node* root ) { if( root != NULL) { Del(root->left); Del(root->right); delete root; //Can't exchange order,can't put delete front or middle } } bool isEqual(node* root1,node* root2) { if ( root1 != NULL && root2 != NULL) { if ( root1->key != root2->key ) return false; return (isEqual(root1->left,root2->left) && isEqual(root1->right,root2->right)); } else { if( root1 == NULL && root2 == NULL ) return true; else return false; } } int main() { int T,i; // string line, stdIn = "", stdPre = "", In = "", Pre = ""; string line; node *root1 = NULL,*root2 = NULL; while(cin >> T && T!=0) { getchar(); //to get '\n' in the end getline(cin,line); root1 = NULL; for(i=0; i<line.size(); ++i) { insert(&root1,new node(line[i])); } // InOrder(root,stdIn); // PreOrder(root,stdPre); // Del(root); // void Memory leaks while( T -- ) { getline(cin,line); root2 = NULL; for(i=0; i<line.size(); ++i) { insert(&root2,new node(line[i])); } if( isEqual(root1,root2) ) cout << "YES" << endl; else cout << "NO" << endl; // In = ""; // InOrder(root,In); // Del(root); // if ( In != stdIn ) // { // cout << "NO" << endl; // continue; // } // root = NULL; // Pre = ""; // PreOrder(root,Pre); // Del(root); // if (Pre != stdPre ) // { // cout <<"NO" << endl; // continue; // } // cout << "YES" << endl; } } return 0; } /* void InOrder(node* root,string& str) { if( root != NULL) { InOrder(root->left, str); str.push_back(root->key); InOrder(root->right, str); } } void PreOrder(node* root,string& str) { if( root != NULL) { str.push_back(root->key); PreOrder(root->left,str); PreOrder(root->right,str); } } */