关于二叉树叶子节点问题
来源:百度知道 编辑:UC知道 时间:2024/06/04 05:37:13
#include<stdio.h>
#define null 0
static int i=0;
struct node
{
long int data;
struct node *left;
struct node *right;
};
int leaf(struct node *t)
{
//定义局部变量
if (t)
{
if(t->left==null&&t->right==null)
i++;
leaf(t->left);
leaf(t->right);
}
return i;
}
/*int leaf(struct node *t)
{
if(t)
{
if(t->left==NULL&&t->right==NULL)
return 1;
else
return (leaf(t->left)+leaf(t->right));
}
return 0; //当t==null的时候返回0;
}*/
int main()
{
int n=0;scanf("%d",&n);
struct node *pp=null,*root=null;
while(n)
{
struct node abc;
scanf("%ld",&abc.data);
n--;
abc.left=null;abc.right=null;
struct node *p=root;
while(p)
{
#define null 0
static int i=0;
struct node
{
long int data;
struct node *left;
struct node *right;
};
int leaf(struct node *t)
{
//定义局部变量
if (t)
{
if(t->left==null&&t->right==null)
i++;
leaf(t->left);
leaf(t->right);
}
return i;
}
/*int leaf(struct node *t)
{
if(t)
{
if(t->left==NULL&&t->right==NULL)
return 1;
else
return (leaf(t->left)+leaf(t->right));
}
return 0; //当t==null的时候返回0;
}*/
int main()
{
int n=0;scanf("%d",&n);
struct node *pp=null,*root=null;
while(n)
{
struct node abc;
scanf("%ld",&abc.data);
n--;
abc.left=null;abc.right=null;
struct node *p=root;
while(p)
{
超时,无法通过评测。
#include <stdio.h>
#include <malloc.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
int leaf(struct node *t)
{
if (t)
{
if(!t->left && !t->right)
return 1;
else
return (leaf(t->left) + leaf(t->right));
}
else
return 0;
}
void clear(struct node *t)
{
if (t)
{
clear(t->left);
clear(t->right);
free(t);
}
}
int main()
{
int data, n;
int sz = sizeof(struct node);
struct node *root;
while (1)
{
root = NULL;
scanf("%d",&n);
if (n < 1 || n > 50000)
break;
struct node *pp, *p, *x;
while (n--)
{
scanf("%d", &data);
pp = NULL;
p = root;
w