#include<iostream>
#include"math.h"
#include"stdlib.h"
#include"string.h"
using namespace std;
typedef struct node
{
char data;
struct node *left;
struct node *right;
}BiNode,*BiTree;
int num=0;
int leafNum = 0;
BiTree creatBiTree();
void PrintNodeAtLevel(BiTree t,int level);
int main()
{
BiTree t=creatBiTree(); //建立
PrintNodeAtLevel(t,2);
printf("该层的结点个数:%d,该层叶子节点数为:%d\n",num,leafNum);
}
//先序建立二叉树 (输入时,按先序次序输入二叉树1中节点的值,以#字符表示孔数)
BiTree creatBiTree()
{
BiTree t;
char ch;
scanf("%c",&ch);
if(ch!='#')
{
t=(BiNode *)malloc(sizeof(BiNode));
t->data=ch;
t->left=creatBiTree();
t->right=creatBiTree();
}
else
t=NULL;
return t;
}
//递归
//参数level代表几层(不是第几层)
void PrintNodeAtLevel(BiTree t,int level)
{
if(t==NULL||level<0)
return;
if(level==0)
{
if(t->left==NULL&&t->right==NULL)
{
leafNum++;
}
num++;
}
PrintNodeAtLevel(t->left,level-1);
PrintNodeAtLevel(t->right,level-1);
}