在刷PAT题目的时候,遇到了一题是判断 两个二叉搜索树是否是同一棵树。
一开始用的方法是最笨的,把两棵树都写出来,比较他们层序遍历之后的结果。测试了之后发现结果没问题,但是提交之后一直出现段错误= - 改了一天都改不掉= - bi狗了。
后来网上学习了别人的方法,可以不用把第二棵树给写出来,只要在节点上加一个flag,比较时每次输入比较后,匹配的就把该位写1就好。
anyway,很深的感悟就是,宁愿花时间去找好的算法,好的解决办法,也不要把时间浪费在对不好的方法或者算法的修改上。
附自己写的程序…哪天有人看到了,说不定能看看段错误出现在哪
#include <stdio.h>
#include <stdlib.h>
typedef struct _node{
int data;
struct _node *left;
struct _node *right;
}Node;
typedef struct _stac{
struct _node *record;
struct _stac *next;
}Stac;
Node* creat_node();
Node* add_tree(Node *root,int number);
Node* creat_tree(int number);
void push_list(Stac *head,Node *node);
Stac* pop_list(Stac *head);
Stac* creat_list();
void byfloor_tree(Node *root,Node *coproot);
void free_tree(Node *root);
void free_list(Stac *head);
int main(int argc, char const *argv[]){ //妈蛋,提交检测的时候总是有段错误,fuck……………………
int times;
int number;
Node *root=NULL;
Node *coproot=NULL;
int i;
int j=0;
scanf("%d",×); //读入,times是树中的节点数,number是需要判断的树的数量
while(times){
scanf("%d",&number);
root=creat_tree(times); //构造初始树(被比较,做为标准的树)
int flag=0;
for(i=0;i<number;i++){
coproot=creat_tree(times); //输入需要判定的树
byfloor_tree(root,coproot); //将需要判定的树的层序遍历结果写入test
free_tree(coproot); //free待测试树
}
free_tree(root);
scanf("%d",×);
}
return 0;
}
void free_tree(Node *root){ //释放树中的每个节点
if(root->left)
free_tree(root->left);
if(root->right)
free_tree(root->right);
free(root);
}
void free_list(Stac *head){
Stac *p=head;
Stac *q=p->next;
while(p){
free(p);
p=q;
if(p){
q=p->next;
}
}
}
void byfloor_tree(Node *root,Node *coproot){ //层序遍历树
int flag=0;
Stac *head=creat_list();
Stac *record=NULL;
Node *mem=NULL;
Node *p=root;
push_list(head,root);
Stac *cophead=creat_list();
Stac *coprecord=NULL;
Node *copmem=NULL;
Node *copp=coproot;
push_list(cophead,coproot);
while(head->next){
record=pop_list(head);
mem=record->record;
push_list(head,mem->left);
push_list(head,mem->right);
coprecord=pop_list(cophead);
copmem=coprecord->record;
push_list(cophead,copmem->left);
push_list(cophead,copmem->right);
if(mem->data != copmem->data){
flag=1;
free(coprecord);
free(record);
break;
}
free(coprecord);
free(record);
}
free_list(head);
free_list(cophead);
if(flag){
printf("No\n");
}
else
{
printf("Yes\n");
}
}
Stac* pop_list(Stac *head){ //记得在调用的时候free 层序遍历时需要用到的出队列
Stac *p=head->next;
if(p){
head->next=p->next;
}
return p;
}
void push_list(Stac *head,Node *node){ //需要对进来的node是否是NUL进行判定,不然会出错= - 层序遍历时需要用到的入队列
Stac *p=head;
Stac *q=creat_list();
if(node){
q->record=node;
while(p->next){
p=p->next;
}
p->next=q;
q->next=NULL;
}
}
Stac* creat_list(){ //制造队头(原来以为是写个栈,所以才取了这么奇怪的名字)
Stac *p=(Stac*)malloc(sizeof(Stac));
p->next=NULL;
p->record=NULL;
}
Node* creat_tree(int times){ //制造树 times是节点数
int number;
Node *root=NULL;
int i=0;
for(i=0;i<times;i++){
scanf("%d",&number); //读入每个树节点的值
if(root==NULL){ //分情况处理节点
root=creat_node();
root->data=number;
}
else
{ //若非根节点,需要判断(四种情况)
add_tree(root,number);
}
}
return root;
}
Node* creat_node(){ //创造树的节点
Node *p=(Node*)malloc(sizeof(Node));
p->data=0;
p->left=NULL;
p->right=NULL;
return p;
}
Node* add_tree(Node *root, int number){//由于不能在函数里面改root的值,所有root的初值需要在main里面给malloc出来
if(!root){
root = creat_node(); //递归查找应该放入的节点,这个是最后一步。
root->data=number;
}
else{
if(number > root->data)
root->right = add_tree(root->right, number);
else
root->left = add_tree(root->left, number);
}
return root;
}