Leaf binary tree of nodes (the list stored in binary)

#include<stdio.h>
#include<stdlib.h>
typedef struct Bitnode
{
    struct Bitnode *left,*right;
    int data;
}Bitnode;
Bitnode *CreatBitree_level()
{
     Bitnode *root=NULL;
    Bitnode *qu[1001];
    int x,front=0,tail=0;
    while(scanf("%d",&x),x!=-1)
    {
        Bitnode *p;
        if(x==0)
        p=NULL;
        else 
        {
            p=(Bitnode*)malloc(sizeof(Bitnode));
            p->data=x;
            p->left=p->right=NULL;
        }
        qu[++tail]=p;
        if(tail==1)
        root=p;
    else    
    {
        if(qu[front]!=NULL&&p!=NULL)
        {
        if(tail%2==0)
        qu[front]->left=p;
        else
            qu[front]->right=p;
        }        
    }
    if(tail%2==1)
        front++;   
    } return root;
}
int leafs(Bitnode *t)
{
    if(t==NULL)
    return 0;
    else if(t->left==NULL&&t->right==NULL)
    return 1;
    else
    {
        return leafs(t->left)+leafs(t->right);
    }
}

 

Guess you like

Origin www.cnblogs.com/zzjam--1/p/11409564.html