数据结构实验之二叉树三:统计叶子数
2016-08-08 11:00
288 查看
题目链接
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char a[150];
int l1;
struct node
{
int data;
struct node *lchild,*rchild;
};
struct node *creat()///先序建立二叉树
{
struct node *root;
char c;
c=a[l1++];
if(c==',')
return NULL;
else
{
root=(struct node *)malloc(sizeof(struct node));
root->data=c;
root->lchild=creat();
root->rchild=creat();
}
return root;
}
int leave(struct node *root)///统计叶子数
{
if(root==NULL)
return 0;
if(root->lchild==NULL&&root->rchild==NULL)
return 1;
else
return leave(root->lchild)+leave(root->rchild);
}
int main()
{
int k;
struct node *root;
while(scanf("%s",a)!=EOF)
{
l1=0;
root=(struct node *)malloc(sizeof(struct node));
root=creat();
k=leave(root);
printf("%d\n",k);
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char a[150];
int l1;
struct node
{
int data;
struct node *lchild,*rchild;
};
struct node *creat()///先序建立二叉树
{
struct node *root;
char c;
c=a[l1++];
if(c==',')
return NULL;
else
{
root=(struct node *)malloc(sizeof(struct node));
root->data=c;
root->lchild=creat();
root->rchild=creat();
}
return root;
}
int leave(struct node *root)///统计叶子数
{
if(root==NULL)
return 0;
if(root->lchild==NULL&&root->rchild==NULL)
return 1;
else
return leave(root->lchild)+leave(root->rchild);
}
int main()
{
int k;
struct node *root;
while(scanf("%s",a)!=EOF)
{
l1=0;
root=(struct node *)malloc(sizeof(struct node));
root=creat();
k=leave(root);
printf("%d\n",k);
}
return 0;
}
相关文章推荐
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- SDUT oj 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 3342 数据结构实验之二叉树三:统计叶子数