学习笔记—二叉树中求度为2的节点个数

这里用具体的代码来看

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

typedef struct Node
{
    char  data;
    struct Node *Lchild;
    struct Node *Rchild;
}BiTNode,*BiTree;//二叉树的数据类型定义

BiTree Creat()//以先序序列创建一棵二叉树
{
    char ch;
    BiTNode *S;
    ch = getchar();
    if(ch=='#')
    {
        return NULL;
    }
    S = (BiTNode *)malloc(sizeof(BiTNode));
    S->data = ch;
    S->Lchild = Creat();
    S->Rchild = Creat();
    return S;
}
int i =0;
void Duer_Bitree(BiTree S)
{
    if(S)//如果节点不为空
    {
        if(S->Lchild!=NULL && S->Rchild !=NULL)//当左右孩子都有的时候证明度为2 则加加
        i++;
        Duer_Bitree(S->Lchild);
        Duer_Bitree(S->Rchild);
    }
}
 int main()
 {
    BiTree T;
    T=Creat();
    getch();
    Duer_Bitree(T);
    printf("%d\n",i);
    getch();
    return 0;
 }

你可能感兴趣的:(二叉树)