计算二叉树的叶子节点写法一:
int countLeaf(BiNode *root)
{
int count=0;
if(root)
{
if(root->lChild==NULL && root->rchild==NULL)
{
count++;
}
if(root->lChild)
{
countLeaf(root->lChild);
}
if(root->rChild)
{
countLeaf(root->rChild);
}
}
return count;
}
copy一棵二叉树
BiNode *CopyTree(BiNode *T)
{
BiNode *newLptr=NULL.*newRptr=NULL;
BiNode *newNode=NULL;
if(T->lchile!=NULL)
{
newLptr=CopyTree(T->lchild);
}
else
{
newLptr=NULL;
}
if(T->Rchile!=NULL)
{
newRptr=CopyTree(T->Rchild);
}
else
{
newRptr=NULL;
}
newNode=(BiNode *)malloc(sizeof(BiNode));
if(newNode==NULL)
{
return NULL;
}
newNode->lchild=newLptr;
newNode->Rchild=newRptr;
newNode->data=T->data;
return newNode;
}
求叶子节点数目
void countLeafNum(BiNode *root,int *num)
{
if(root!=NULL)
{
countLeafNum(root->lchild);//递归遍历左子树
if(root->lchild==NULL&&root->rchild==NULL)
{
(*num)++;
}
countLeafNum(root->rchild);//递归遍历右子树
}
}
求二叉树的深度
int GetTreeDeapth(BiNode *root)
{
int deapth=0,ldeapth=0,rdeapth=0;
if(r00t=NULL)
{
deapth=0;
return deapth
}
ldeapth=GetTreeDeapth(root->lchild);
rdeapth=GetTreeDeapth(root->rchild);
deapth=1+((ldeapth>rdeapth)?ldeapth:rdeapth);
return deapth;
}