1.递归判断相等
#include "stdafx.h"
#include<iostream>
using namespace std;
typedef struct BTreeNode
{
int data;
struct BTreeNode *lchild,*rchild;
}BTree;
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
bool equal(BTree *p,BTree *q)
{
if(!p&&!q)return true;//若都为空
else if(!p&&q||p&&!q||p->data!=q->data)
return false;
else return(equal(p->lchild,q->rchild),equal(p->rchild,q->rchild));
}
2.递归复制
BTree *Copy(BTree *t)
{
BTree *bt;
if(t==NULL)bt=NULL;//若为空
else
{
bt=(BTree*)malloc(sizeof(BTreeNode));
bt->data=t->data;
bt->lchild=Copy(t->lchild);
bt->rchild=Copy(t->rchild);
}
return bt;
}
3.非递归遍历
BTree *Copy(BTree *t)
{
typedef struct
{
BTree *t;
BTree *bt;
} node;
BTree *bt;
node q[100];
if(t==NULL){bt=NULL;return bt;}//若为空
else
{
QueueIn(Q,(t,bt));
while(!QueueEmpty(Q))
{
(t,bt)=QueueOut(Q);
bt=(BTree*)malloc(sizeof(BTreeNode));
bt->data=t->data;
if(t->lchild)QueueIn(Q,(t->lchild,bt->lchild));else bt->lchild=NULL;
if(t->rchild)QueueIn(Q,(t->rchild,bt->lchild));else bt->rchild=NULL;
}
return bt;
}