您的位置:首页 > 其它

hdu 3791(动态内存的释放问题)未解决

2011-10-12 12:12 295 查看
View Code

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node{
char num;
struct node *left;
struct node *right;
//  node():left(NULL),right(NULL){}
};
node *build(node *root,char ch)
{
if(root==NULL)
{
root=new node;
root->left=NULL;
root->right=NULL;
root->num=ch;
return root;
}
else
{
if(ch < (root->num))
root->left=build(root->left,ch);
else
root->right=build(root->right,ch);
return root;
}
}
bool is_same( node*Main_tree, node*sub_tree)
{
if( Main_tree== NULL || sub_tree== NULL)return true;
if( Main_tree->num!= sub_tree->num) {
return false;
}else{
if( is_same( Main_tree->left, sub_tree->left) ) {
return is_same( Main_tree->right, sub_tree->right);
}else{
return false;
}
}
}
void destory(node *root)
{
if(root!=NULL)
{
if(root->left!=NULL)  {root->num;
destory(root->left);}
delete root;{root->num;}
if(root->right!=NULL) {root->num;
destory(root->right);}
}
}
void   DeleteTree( node   *Root)
{
struct   node   *pointer=new node;
pointer=Root;
if(pointer!=NULL)
{
DeleteTree(pointer-> left);
delete pointer;
DeleteTree(pointer-> right);
}
}
int main()
{
int n;
int i,j;
char str[20];
while(scanf("%d",&n),n)
{
node *tree1=NULL,*tree2=NULL;
//tree1=new node;
if(tree1!=NULL) printf("!=null");
scanf("%s",str);
int len=strlen(str);
for(i=0;i<len;i++) {
tree1=build(tree1,str[i]);
}
for(i=0;i<n;i++)
{
scanf("%s",str);
len=strlen(str);
for(j=0;j<len;j++)
tree2=build(tree2,str[j]);
if(is_same(tree1,tree2)) printf("YES\n");
else printf("NO\n");
//  destory(tree2);
DeleteTree(tree2);
}
destory(tree1);
}
return 0;
}

/*
2
567432
576342
543267

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: