计算二叉树的深度
//后序遍历的基础
int Depth(BiTree T)
{
if(T = NULL)
{
return 0;
}
else
{
m = Depth(T->lchild);
n = Depth(T->rchild);
//二叉树的深度为m与n中较大者加1
if(m > n)
{
return m+1;
}
esle
{
return n+1;
}
}
}
统计二叉树中结点的个数
int NodeCount(BiTree T)
{
if(T == NULL)
{
return 0;
}
else
{
m = NodeCount(T->lchild);
n = NodeCount(T->rchild);
return m + n + 1;
}
}
先序遍历复制二叉树
void Copy(BiTree T, BiTree *NewT)
{
if(T==NULL)
{
*NewT = NULL;
}
else
{
*NewT = new BiTNode;
(*NewT)->data = T->data;
Copy(T->lchild, NewT->lchild);
Copy(T->rchild, NewT->rchild);
}
}
先序遍历建立二叉链表
void CreateBiTree(BiTree *T)
{
cin>>ch;
if(ch == '#')
{
*T = NULL;
}
else
{
*T = new BiTNode;
(*T)->data = ch;
CreateBiTree((*T)->lchild);
CreateBiTree((*T)->rchild);
}
}