求二叉树有多少个度为2、0的结点

#include<stdio.h>
#include<malloc.h>
//2013-12-23 
//乾卦
#define MAX 256
typedef struct Node
{
    char data;
    struct Node *rchild;
    struct Node *lchild;
}BTNode;
//创建二叉树
void CreatBT(BTNode *&root,char *str)
{
    int top=0;
    int j=0;
    BTNode *st[MAX];
    BTNode *p=NULL;
    int flg=0;
    root=NULL;
    while(str[j]!='')
    {
        if(str[j]=='(')
        {
            top++;
            st[top]=p;
            flg=1;
        }
        else if(str[j]==')')
        {
            top--;
        }
        else if(str[j]==',')
        {
            flg=2;
        }
        else
        {
            p=(BTNode*)malloc(sizeof(BTNode));
            if(!p)
            {
                printf("申请空间失败!
");
                return ;
            }
            p->data=str[j];
            p->lchild=NULL;
            p->rchild=NULL;
            if(!root)
            {
                root=p;
            }
            else
            {
                if(flg==1)
                    st[top]->lchild=p;
                if(flg==2)
                    st[top]->rchild=p;
            }
        }
        j++;
    }

    return ;
}
//求二叉树有多少个度为2的结点
int Func(BTNode *root)
{
    int flg=0;
    int count=0;
    BTNode* queue[MAX];
    BTNode* p=NULL;
    int rear=-1,front=-1;

    if(!root)
        return -1;
    rear++;
    queue[rear]=root;
    while(rear!=front)
    {
        front++;
        p=queue[front];
        flg=0;
        if(p->lchild)
        {
            //进队
            rear++;
            queue[rear]=p->lchild;
            flg++;
        }
        if(p->rchild)
        {
            rear++;
            queue[rear]=p->rchild;
            flg++;
        }
        if(2==flg)
            count++;
    }

    return count;
}

void main()
{
    int i;
    BTNode *ROOT=NULL;
    CreatBT(ROOT,"A(B(D(,G)),C(E,F))");
    i=Func(ROOT);
    printf("一共有%d个度为2的结点
",i);
    //完全二叉树 1+2+1=4
    CreatBT(ROOT,"A(B(D(H,I),E),C(F,G))");
    i=Func(ROOT);
    printf("一共有%d个度为2的结点
",i);
    //高度为4的满二叉树 1+2+4=7
    CreatBT(ROOT,"A(B(D(H,I),E(J,K)),C(F(L,M),G(N,O)))");
    i=Func(ROOT);
    printf("一共有%d个度为2的结点
",i);
    //0
    CreatBT(ROOT,"A(,B(C(,D(E))))");
    i=Func(ROOT);
    printf("一共有%d个度为2的结点
",i);
    //0
    CreatBT(ROOT,"A(B,)");
    i=Func(ROOT);
    printf("一共有%d个度为2的结点
",i);
    //
    CreatBT(ROOT,"A(B(D,E),C(F(H(J,K),I),G))");
    i=Func(ROOT);
    printf("一共有%d个度为2的结点
",i);
    return ;
}

#include<stdio.h>
#include<malloc.h>
//2013-12-24 
//乾卦
#define MAX 256
typedef struct Node
{
    char data;
    struct Node *rchild;
    struct Node *lchild;
}BTNode;
//创建二叉树
void CreatBT(BTNode *&root,char *str)
{
    int top=0;
    int j=0;
    BTNode *st[MAX];
    BTNode *p=NULL;
    int flg=0;
    root=NULL;
    while(str[j]!='')
    {
        if(str[j]=='(')
        {
            top++;
            st[top]=p;
            flg=1;
        }
        else if(str[j]==')')
        {
            top--;
        }
        else if(str[j]==',')
        {
            flg=2;
        }
        else
        {
            p=(BTNode*)malloc(sizeof(BTNode));
            if(!p)
            {
                printf("申请空间失败!
");
                return ;
            }
            p->data=str[j];
            p->lchild=NULL;
            p->rchild=NULL;
            if(!root)
            {
                root=p;
            }
            else
            {
                if(flg==1)
                    st[top]->lchild=p;
                if(flg==2)
                    st[top]->rchild=p;
            }
        }
        j++;
    }

    return ;
}

int SumLeaf(BTNode *root)
{
    int flg=0;
    int count1=0,count2=0,count_sum=0;
    
    BTNode* queue[MAX];
    BTNode* p=NULL;
    int rear=-1,front=-1;

    if(!root)
        return -1;
    rear++;
    queue[rear]=root;
    while(rear!=front)
    {
        
        front++;
        p=queue[front];
        //计算总结点数
        count_sum++;
        flg=0;
        if(p->lchild)
        {
            //进队
            rear++;
            queue[rear]=p->lchild;
            flg++;
        }
        if(p->rchild)
        {
            rear++;
            queue[rear]=p->rchild;
            flg++;
        }
        if(1==flg)
        {
            count1++;
            
        }
        if(2==flg)
        {
            count2++;
        }
    }
    //总结点-结点数为1的-结点数为2的
    return (count_sum-count2-count1);
}

void main()
{
    int i;
    BTNode *ROOT=NULL;
    CreatBT(ROOT,"A(B(D(,G)),C(E,F))");
    i=SumLeaf(ROOT);
    printf("一共有%d个度为0的结点
",i);
    //高度为4的满二叉树 1+2+4=7
    CreatBT(ROOT,"A(B(D(H,I),E(J,K)),C(F(L,M),G(N,O)))");
    i=SumLeaf(ROOT);
    printf("一共有%d个度为0的结点
",i);
    return ;
}

比上面简单的算法:

#include<stdio.h>
#include<malloc.h>
//2013-12-24 
//乾卦
#define MAX 256
typedef struct Node
{
    char data;
    struct Node *rchild;
    struct Node *lchild;
}BTNode;
//创建二叉树
void CreatBT(BTNode *&root,char *str)
{
    int top=0;
    int j=0;
    BTNode *st[MAX];
    BTNode *p=NULL;
    int flg=0;
    root=NULL;
    while(str[j]!='')
    {
        if(str[j]=='(')
        {
            top++;
            st[top]=p;
            flg=1;
        }
        else if(str[j]==')')
        {
            top--;
        }
        else if(str[j]==',')
        {
            flg=2;
        }
        else
        {
            p=(BTNode*)malloc(sizeof(BTNode));
            if(!p)
            {
                printf("申请空间失败!
");
                return ;
            }
            p->data=str[j];
            p->lchild=NULL;
            p->rchild=NULL;
            if(!root)
            {
                root=p;
            }
            else
            {
                if(flg==1)
                    st[top]->lchild=p;
                if(flg==2)
                    st[top]->rchild=p;
            }
        }
        j++;
    }

    return ;
}
//求二叉树有多少个度为0的结点
int _SumLeaf(BTNode *root)
{
    int flg=0;
    int count=0;
    BTNode* queue[MAX];
    BTNode* p=NULL;
    int rear=-1,front=-1;

    if(!root)
        return -1;
    rear++;
    queue[rear]=root;
    while(rear!=front)
    {
        front++;
        p=queue[front];
        flg=0;
        if(p->lchild)
        {
            //进队
            rear++;
            queue[rear]=p->lchild;
            flg++;
        }
        if(p->rchild)
        {
            rear++;
            queue[rear]=p->rchild;
            flg++;
        }
        if(0==flg)
            count++;
    }

    return count;
}


void main()
{
    int i;
    BTNode *ROOT=NULL;
    CreatBT(ROOT,"A(B(D(,G)),C(E,F))");
    i=_SumLeaf(ROOT);
    printf("一共有%d个度为0的结点
",i);
    //高度为4的满二叉树 
    CreatBT(ROOT,"A(B(D(H,I),E(J,K)),C(F(L,M),G(N,O)))");
    i=_SumLeaf(ROOT);
    printf("一共有%d个度为0的结点
",i);
    return ;
}
原文地址:https://www.cnblogs.com/qiangua/p/3487984.html