代码如下 最好要把它弄懂 否则就没意义了 加油!
#include
using namespace std;
struct tree
{
int data;
tree *lchild,*rchild;
};
class Ttree
{
private:
tree *root;
tree *creat(tree *bt);
public:
Ttree(){root=creat(root);}
int depth(tree *root);
int depth(){int h2;h2=depth(root);return h2;}
};
tree *Ttree::creat(tree *bt)
{
char ch;
cin>>ch;
if(ch=='#')bt=NULL;
else
{
bt=new tree;
bt->data=ch;
bt->lchild=creat(bt->lchild);
bt->rchild=creat(bt->rchild);
}
return bt;
}
int Ttree::depth(tree *bt)
{
int zuo, you,h;//分别代表 左树的层数 右树的层数 和深度
if(bt==NULL)
{
return 0;
}
else
{
zuo=depth(bt->lchild);
you=depth(bt->rchild);
h=1+(zuo>you?zuo:you);
}
return h;
}
int main()
{
int f=1;
while(f!=0)
{
Ttree b;
f=b.depth();cout |