zoukankan      html  css  js  c++  java
  • 6_42_二叉树递归求叶子节点个数

    #include<stdio.h>
    #include<stdlib.h>
    #include<malloc.h>
    typedef struct node
    {
        int data;
        struct node*lchild,*rchild;
    }tnode,*tree;
    tree creat()
    {
        int x;
        tree t;
        scanf("%d",&x);
        if(x==0)t=NULL;
        else
        {
            t=(tnode*)malloc(sizeof(tnode));
            t->data=x;
            t->lchild=creat();
            t->rchild=creat();
        }
        return t;
    }
    int LeafNum(tree t)
    {
        if(t)
        {
            if(!t->lchild&&!t->rchild)
                return 1;
            return LeafNum(t->lchild)+LeafNum(t->rchild);
        }
        return 0;
    }
    int main()
    {
        tree t=creat();
        printf("%d
    ",LeafNum(t));
    }
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    MySQL数据库基础
    Django框架
    Python基础
    C#
    小功能
    数据结构与算法
    C语言
    Robot Framework高级
    Robot Framework初级
    C++基础
  • 原文地址:https://www.cnblogs.com/Thereisnospon/p/4768473.html
Copyright © 2011-2022 走看看