69,378
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<stdlib.h>
typedef struct Bin_Tree_Node{
struct Bin_Tree_Node *left_child;
struct Bin_Tree_Node *right_child;
int data;
}Bin_Tree_Node;
//将输入的序列按照二叉树的先序遍历创建二叉树
void CreateBinTree( Bin_Tree_Node **p_root )
{
static int input_data;
scanf("%d", &input_data );
if( input_data == -1 )
{
(*p_root) = NULL;
return;
}
else
{
(*p_root) = (Bin_Tree_Node *)malloc( sizeof(Bin_Tree_Node) );
(*p_root)->data = input_data;
CreateBinTree( &(*p_root)->left_child );
CreateBinTree( &(*p_root)->right_child );
}
}
//求二叉树中节点的个数
int BinTreeNodeCount( Bin_Tree_Node *root )
{
int num, num1, num2;
if( root==NULL )
{
return 0;
}
else
{
num1 = BinTreeNodeCount( root->left_child );
num2 = BinTreeNodeCount( root->right_child );
num = num1 + num2 + 1;
return num;
}
}
int main()
{
Bin_Tree_Node *root;
int node_num;
CreateBinTree( &root );
node_num = BinTreeNodeCount( root );
printf("\nnumber of node: %d \n", node_num );
system("pause");
return 0;
}
main()
{
int num=0 ; //计数
…………
Node_Num(T, &num);
}
void Node_Num(BT T, int *num)
{
if(T) {
(*num)++ ;
Node_Num(T->lchild, num) ;
Node_Num(T->rchild, num) ;
} else
return ;
}