上代码
#include <iostream>
using namespace std;
#include <string>
typedef struct BiTNode
{
string data;
struct BiTNode *lchild, *rchild;
BiTNode()
{
data = "";
lchild = NULL; //给创建的指针赋初值,消灭野指针
rchild = NULL;
}
}BiTNode, *BiTree;
//复制
int Copy(BiTree T, BiTree &NewT)
{
if (T == NULL)
{
NewT = NULL;
return 0;
}
else
{
NewT->data = T->data;
Copy(T->lchild, NewT->lchild);
Copy(T->rchild, NewT->rchild);
}
}
//计算深度
int Depth(BiTree T)
{
if (T == NULL)
return 0;
else
{
int m = Depth(T->lchild);
int n = Depth(T->rchild);
if (m > n)return (m + 1);
else return (n + 1);
}
}
//计算总结点数
int NodeCount(BiTree T)
{
if (T == NULL) return 0;
else
return (NodeCount(T->lchild) +
NodeCount(T->lchild) + 1);
}
//计算叶子节点数
int LeafCount(BiTree T)
{
if (T == NULL) return 0;
if (T->lchild == NULL && T->rchild == NULL)
return 1;
else
return (LeafCount(T->lchild) + LeafCount(T->rchild));
}
int main()
{
system("pause");
}
最后,再次感谢王卓老师在B站的优质课程数据结构