寻找叶子节点

添加链接描述

/**/
#include<stdio.h>
#include<stdlib.h>
typedef struct node * BT;
int top,count;
char s[55];
struct node
{
char str;
BT left;
BT right;
};
BT creat()
{
BT root;

top++;
if(s[top]==',')
    root=NULL;
else
{ root=(BT)malloc(sizeof(struct node));
    root->left=creat();
    root->right=creat();
    root->str=s[top];
}
return root;

}
void ser(BT tree)
{
if(tree)
{
if(!tree->left&&!tree->right)//没有左孩子且没有右孩子,
count++; //叶子节点数加一
ser(tree->left);
ser(tree->right);
}

}
int main()
{
BT tree;
while(~scanf("%s",s))
{
top=-1;
count=0;
tree=creat();
ser(tree);
printf("%d\n",count);
}
return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44145887/article/details/88778457