#include <stdlib.h>
#include <stdio.h>
typedef struct
{
struct Node* pLeft;
struct Node* pRight;
int iValue;
}
Node;
int CountTreeLeaf( Node* pNode )
{
if( !pNode )
return 0;
if( !pNode->pLeft && !pNode->pRight )
return 1;
return CountTreeLeaf( pNode->pLeft ) + CountTreeLeaf( pNode->pRight );
}
int main( int argc, char** argv )
{
Node* pHeader = (Node*)malloc( sizeof( Node ) );
if( !pHeader )
return -1;
Node* pCur = pHeader;
Node* pTmp = NULL;
pCur->iValue = 1;
pCur->pLeft = NULL;
pCur->pRight = NULL;
pCur->pLeft = (Node*)malloc( sizeof( Node ));
//pCur->pRight = (Node*)malloc( sizeof( Node ));
pTmp = pCur->pLeft;
pTmp->iValue = 2;
pTmp->pLeft = NULL;
pTmp->pRight = NULL;
//pTmp = pCur->pRight;
//pTmp->iValue = 3;
//pTmp->pLeft = NULL;
//pTmp->pRight = NULL;
puts( "" );
printf( "The leaves of the binary tree: %d\n", CountTreeLeaf( pHeader ));
puts( "" );
return 0;
}
二叉树叶子数
最新推荐文章于 2022-07-03 11:14:32 发布