zoukankan      html  css  js  c++  java
  • 判断任一二叉树,是否为满二叉树.(输出二叉树,节点总数,二叉树深度)

    #include "stdio.h"
    #include "malloc.h"
    int count;
    typedef struct node
    {
     char data;
     struct node *LChild;
     struct node *RChild;
    }BiTNode,*BiTree;

    void creatbitree(BiTree * bt) // 先序便历序列创建二叉树链表
    {
     char ch=getchar();
     if (ch=='#')
     {
      *bt=NULL;
     }
     else
     {
      *bt=(BiTree)malloc(sizeof(BiTNode));
      (*bt)->data=ch;
      creatbitree(&((*bt)->LChild));
      creatbitree(&((*bt)->RChild));


     }
    }

    int ProOrder(BiTree root)  // 先序便历输出二叉树节点
    {
     if (root!=NULL)
     {
      printf("%c ",root->data);  // 输出根节点
      count++;
      ProOrder(root->LChild);
      ProOrder(root->RChild);
     }
     return count;

    }
    int PostTreeDepth(BiTree bt) // 后序便历求二叉树的高度
    {
     int hl,hr,max;
     if (bt!=NULL)
     {
      hl=PostTreeDepth(bt->LChild);
      hl=PostTreeDepth(bt->LChild);
      max=hl>hr?hl:hr ;
      return (max+1) ;

     }
     else
      return 0;
    }
    void Judgment(int m,int n)  // 判断该二叉树是否为满二叉树
    {
     int s=1;
     for (int i=1;i<=n;i++)
     {
      s=s*2;
     }
     if (s-1==m)
     {
      printf("是满二叉树! ");

     }
     else
     { 
      printf("不是满二叉树! ");
     }
    }

    void main()
    {
     int c,h;
     count=0;
     BiTree root;
     creatbitree(&root);
     c=ProOrder(root);
     h=PostTreeDepth(root);
     printf("节点的总数:%d ",count);
     printf("二叉树的深度:%d ",h);
     Judgment(c,h);

    }

    原CSDN链接地址:http://blog.csdn.net/sha_520/article/details/7397534

  • 相关阅读:
    数据结构
    java web
    C++
    SQL(结构化查询语言)
    网站协议
    python
    爬虫
    select 多选
    List 去除重复数据的五种方式
    oracle锁表SID查询
  • 原文地址:https://www.cnblogs.com/viaiu/p/4806216.html
Copyright © 2011-2022 走看看