算法思想

统计二叉树中叶子结点的个数和度为1、度为2的结点个数,因此可以参照二叉树三种遍历算法(先序、中序、后序)中的任何一种去完成,只需将访问操作具体变为判断是否为叶子结点和度为1、度为2的结点及统计操作即可。

Code

#include <stdio.h>
#include <stdlib.h> int LeafCount=;
int Degree1Count=;
int Degree2Count=; typedef char DataType; //二叉链表结点的数据类型 typedef struct Node //定义二叉树的二叉链表结点结构
{
DataType data;
struct Node *LChild; //左子树
struct Node *RChild; //右子树
}BiTNode,*BiTree; void CreateBiTree(BiTree *bt); //创建二叉链表函数
void PreOrder(BiTree root); //先序遍历二叉树
void InOrder(BiTree root); //中序遍历二叉树
void PostOrder(BiTree root); //后序遍历二叉树
void Leaf(BiTree root); //统计叶子结点数目
void Degree1(BiTree root); //统计度为1的结点数目
void Degree2(BiTree root); //统计度为2的结点数目 int main()
{
BiTree bt;
int choice;
while(true)
{ //二叉树操作选择菜单
printf("*****************Please enter your choice*****************\n\n");
printf(" choice 1:创建二叉树\n");
printf(" choice 2:先序遍历二叉树\n");
printf(" choice 3:中序遍历二叉树\n");
printf(" choice 4:后序遍历二叉树\n");
printf(" choice 5:打印叶子结点数目\n");
printf(" choice 6:打印度为1的结点数目\n");
printf(" choice 7:打印度为2的结点数目\n");
printf(" choice 0:退出\n\n");
scanf("%d",&choice);
switch(choice)
{
case : CreateBiTree(&bt);
break;
case : PreOrder(bt);
printf("\n");
break; case : InOrder(bt);
printf("\n");
break;
case : PostOrder(bt);
printf("\n");
break;
case : Leaf(bt);
printf("该二叉树叶子结点的数目为:%d\n",LeafCount);
break;
case : Degree1(bt);
printf("该二叉树度为1的结点数目为:%d\n",Degree1Count);
break;
case : Degree2(bt);
printf("该二叉树度为2的结点数目为:%d\n",Degree2Count);
break;
case : exit();
break;
default:
printf("ERROR!!\n");
exit();
break;
}
}
return ;
} void CreateBiTree(BiTree *bt)
{
char ch;
printf("Please enter data:");
getchar();
ch = getchar();
if(ch == '.') //读入的数据是'.'则将当前树根置为空
{
*bt = NULL;
}
else //读入正常数据,为当前树根分配地址空间
{
*bt = (BiTree)malloc(sizeof(BiTNode));
(*bt)->data = ch;
CreateBiTree(&((*bt)->LChild)); //递归调用CreateBiTree()函数,处理左子树
CreateBiTree(&((*bt)->RChild)); //递归调用CreateBiTree()函数,处理右子树
}
} void PreOrder(BiTree root) //先序遍历二叉树,root为指向二叉树根结点的指针
{
if(root!=NULL)
{
printf("%c ",root->data); //访问根结点
PreOrder(root->LChild); //先序遍历左子树
PreOrder(root->RChild); //先序遍历右子树
}
} void InOrder(BiTree root) //中序遍历二叉树,root为指向二叉树根结点的指针
{
if(root!=NULL)
{
InOrder(root->LChild); //中序遍历左子树
printf("%c ",root->data); //访问根结点
InOrder(root->RChild); //中序遍历右子树
}
} void PostOrder(BiTree root) //中序遍历二叉树,root为指向二叉树根结点的指针
{
if(root!=NULL)
{
PostOrder(root->LChild); //后序遍历左子树
PostOrder(root->RChild); //后序遍历右子树
printf("%c ",root->data); //访问根结点
}
} void Leaf(BiTree root)
{
if(root!=NULL)
{
Leaf(root->LChild);
Leaf(root->RChild);
if(root->LChild==NULL && root->RChild==NULL)
{
LeafCount++; //统计叶子结点数目
}
}
} void Degree1(BiTree root)
{
if(root!=NULL)
{
Degree1(root->LChild);
Degree1(root->RChild);
if((root->LChild==NULL && root->RChild!=NULL)||(root->LChild!=NULL && root->RChild==NULL))
{
Degree1Count++; //统计度为1的结点数目
}
}
} void Degree2(BiTree root)
{
if(root!=NULL)
{
Degree2(root->LChild);
Degree2(root->RChild);
if(root->LChild!=NULL && root->RChild!=NULL)
{
Degree2Count++; //统计度为2的结点数目
}
}
}

C语言实现二叉树中统计叶子结点的个数&度为1&度为2的结点个数的更多相关文章

  1. Morris Traversal方法遍历二叉树(非递归,不用栈,O(1)空间)——无非是在传统遍历过程中修改叶子结点加入后继结点信息(传统是stack记录),然后再删除恢复

    先看看线索二叉树 n个结点的二叉链表中含有n+1(2n-(n-1)=n+1)个空指针域.利用二叉链表中的空指针域,存放指向结点在某种遍历次序下的前驱和后继结点的指针(这种附加的指针称为"线索 ...

  2. C语言实现二叉树-利用二叉树统计单词数目

    昨天刚参加了腾讯2015年在线模拟考: 四道大题的第一题就是单词统计程序的设计思想: 为了记住这一天,我打算今天通过代码实现一下: 我将用到的核心数据结构是二叉树: (要是想了解简单二叉树的实现,可以 ...

  3. 【Leetcode】查找二叉树中任意结点的最近公共祖先(LCA问题)

    寻找最近公共祖先,示例如下: 1 /           \ 2           3 /    \        /    \ 4    5      6    7 /    \          ...

  4. [LeetCode] Closest Leaf in a Binary Tree 二叉树中最近的叶结点

    Given a binary tree where every node has a unique value, and a target key k, find the value of the n ...

  5. 求二叉树中第K层结点的个数

    一,问题描述 构建一棵二叉树(不一定是二叉查找树),求出该二叉树中第K层中的结点个数(根结点为第0层) 二,二叉树的构建 定义一个BinaryTree类来表示二叉树,二叉树BinaryTree 又是由 ...

  6. 用C语言把双向链表中的两个结点交换位置,考虑各种边界问题。

    用C语言把双向链表中的两个结点交换位置,考虑各种边界问题. [参考] http://blog.csdn.net/silangquan/article/details/18051675

  7. 二叉树中序遍历 (C语言实现)

    在计算机科学中,树是一种重要的非线性数据结构,直观地看,它是数据元素(在树中称为结点)按分支关系组织起来的结构.二叉树是每个节点最多有两个子树的有序树.通常子树被称作“左子树”(left subtre ...

  8. [LeetCode] Second Minimum Node In a Binary Tree 二叉树中第二小的结点

    Given a non-empty special binary tree consisting of nodes with the non-negative value, where each no ...

  9. Leetcode 863. 二叉树中所有距离为 K 的结点

    863. 二叉树中所有距离为 K 的结点  显示英文描述 我的提交返回竞赛   用户通过次数39 用户尝试次数59 通过次数39 提交次数174 题目难度Medium 给定一个二叉树(具有根结点 ro ...

随机推荐

  1. 解决iPhone Safari 兼容性CSS背景显示不全问题

    https://jingyan.baidu.com/article/ca2d939d014ccbeb6c31ceb7.html 看到了这个文章解决的.中心部分小于980的时候回出现.苹果手机中的saf ...

  2. IE控件cab包手动安装

    一.XP系统 第1步:先解压cab包,在解压的文件中找到*.inf文件,然后右击,选择安装,此时会把解压文件拷到C:Windows\System32文件夹下.第2步:注册拷到上述文件夹下的ocx文件. ...

  3. vue相关文件说明(基于vue2.0)

    1.config:生产,开发环境配置参数 2.static:第三方资源,这里面的文件直接写路径,不能用'import'导入 3.node_modules:引入一些依赖包 4..babelrc:定义了E ...

  4. [LeetCode] Find And Replace in String 在字符串中查找和替换

    To some string S, we will perform some replacement operations that replace groups of letters with ne ...

  5. python编程从入门到实战1-3章

    print('hellow world') """ 多行注释"""#大小写print('i love you')mssage='hellow ...

  6. linux操作命令之搜索命令

    1.文件搜索命令:locate 文件名 在后台数据库中按照文件名搜索,搜素速度更快 /var/lib/mlocate:#locate命令所搜索的后台数据库 updatedb:更新数据库 updated ...

  7. H5 和移动端 WebView 缓存机制解析与实战

    本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/qHm_dJBhVbv0pJs8Crp77w 作者:叶 ...

  8. 【RL-TCPnet网络教程】第5章 PHY芯片和STM32的MAC基础知识

    第5章        PHY芯片和STM32的MAC基础知识 本章节为大家讲解STM32自带的MAC和PHY芯片的基础知识,为下一章底层驱动的讲解做一个铺垫. 5.1   初学者重要提示 5.2    ...

  9. JAVA基础—线程池

    推荐文章java多线程基础 线程池概述 为什么要使用线程池 1.服务器创建和销毁工作线程的开销很大 2.如果频繁的创建和销毁线程会导致频繁的切换线程,因为一个线程被销毁后,必然要把CPU转让给另一个已 ...

  10. [Swift]LeetCode108. 将有序数组转换为二叉搜索树 | Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...