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

来源:互联网 发布:淘宝号信誉度查询 编辑:程序博客网 时间:2024/05/19 18:14

这里用具体的代码来看

#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; }
0 0