1.二叉树递归遍历
//先序遍历
void PreOrder(BinTree root)
{
if( root != NULL) { //非空二叉树
printf("%c", root->data); //访问根节点
PreOrder(root->LChild); //递归遍历左子树
PreOrder(root->RChild); //递归遍历右子树
}
}
//中序遍历
void InOrder(BinTree root)
{
if( root != NULL) {
InOrder(root->LChild);
printf("%c", root->data);
InOrder(root->RChild);
}
}
//后序遍历
void PostOrder(BinTree root)
{
if( root != NULL) {
PostOrder(root->LChild);
PostOrder(root->RChild);
printf("%c", root->data);
}
}
2.二叉树遍历应用
//统计结点总数
int Count(BinTree T)
{
if(T == NULL) {
return 0;
}
else {
return( Count(T->LChild) + Count(T->RChild) + 1 );
}
}
//统计叶子结点总数
int LeafCount(BinTree T)
{
if(T == NULL){
return 0;
}
else if(T->LChild == NULL && T->RChild == NULL){
return 1;
}
else {
return( LeafCount(T->LChild) + LeafCount(T->RChild) );
}
}