tree 先序遍历 叶子结点_先序遍历创建二叉树,对二叉树统计叶子节点个数和统计深度(创建二叉树时#代表空树,序列不能有误)c语言...

该博客通过C语言实现先序遍历创建二叉树,其中'#'代表空节点。同时,提供两个函数分别统计二叉树的叶子节点个数和深度。在主函数中,展示了如何使用这两个函数并打印结果。
摘要由CSDN通过智能技术生成

#include "stdio.h"

#include "string.h"

#include "malloc.h"

#define NULL 0

#define MAXSIZE 30

typedef struct BiTNode      //定义二叉树数据结构

{

char data;

struct BiTNode *lchild,*rchild;

} BiTNode;

void preCreate(BiTNode *& T)   //先序遍历建立二叉树,#代表空树

{

char ch;

ch=getchar();

if(ch=='#')

T=NULL;

else

{

if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))

printf("Error!");

T->data=ch;

preCreate(T->lchild);

preCreate(T->rchild);

}

}

int getLeafNum(BiTNode *root)//统计二叉树叶子节点个数

{

int count=0;//叶子总数,左子树叶子数.右子数叶子数

int left_count=0;

int right_count=0;

/*推断根节点是否为null

若根节点不空,推断根节点是否是叶子。是的话叶子总数+1并返回,

若不是统计左子树叶子数目和右子数叶子数目并相加返回

若根节点为空,则叶子数为0并返回

*/

if(root)

{

if(root->lchild==NULL&&root->rchild==NULL)

count++;

else

{

left_count=getLeafNum(root->lchild);

right_count=getLeafNum(root->rchild);

count=left_count+right_count;

}

}

else

{

count=0;

}

return count;

}

int getTreeDepth(BiTNode *root)//统计二叉树深度

{

int depth=0;

int left_depth=0;

int right_depth=0;

/*

推断根节点是否为空,

若根节点为空,深度置为0,并返回

若根节点不为空,统计左子树深度,统计右子树深度,二者相加后再加上1(1位根节点)并返回

*/

if(root)

{

left_depth=getTreeDepth(root->lchild);

right_depth=getTreeDepth(root->rchild);

depth=1+(left_depth>right_depth?

left_depth:right_depth);

}

else

{

depth=0;

}

return depth;

}

int main()

{

BiTNode * bitree=NULL;

preCreate(bitree);//先序遍历创建二叉树

printf("叶子个数:%d\n",getLeafNum(bitree));

printf("该二叉树深度:%d\n",getTreeDepth(bitree));

return 0;

}

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
先序遍历序列建立二叉树的步骤: 1. 从先序遍历序列中取出第一个元素作为根节点。 2. 剩余的序列中,找到第一个大于根节点的值,它左边的部分是根节点的左子树的先序遍历序列,右边的部分是根节点的右子树的先序遍历序列。 3. 对左子树序列和右子树序列分别递归进行上述步骤。 代码如下: ```python class TreeNode: def __init__(self, val): self.val = val self.left = None self.right = None def build_tree(preorder): if not preorder: return None root = TreeNode(preorder[0]) i = 1 while i < len(preorder) and preorder[i] < root.val: i += 1 root.left = build_tree(preorder[1:i]) root.right = build_tree(preorder[i:]) return root ``` 统计二叉树叶子结点个数二叉树深度可以通过递归实现。叶子结点的定义是没有左右子树的节点,因此可以判断当前节点是否为叶子节点,如果是,则返回1,否则返回左右子树的叶子结点个数之和。二叉树深度可以定义为根节点到最深叶子节点的路径长度,因此可以递归计算左右子树的深度,然后取较大值加1即可。 代码如下: ```python def count_leaf(root): if not root: return 0 if not root.left and not root.right: return 1 return count_leaf(root.left) + count_leaf(root.right) def max_depth(root): if not root: return 0 return max(max_depth(root.left), max_depth(root.right)) + 1 ``` 完整代码如下: ```python class TreeNode: def __init__(self, val): self.val = val self.left = None self.right = None def build_tree(preorder): if not preorder: return None root = TreeNode(preorder[0]) i = 1 while i < len(preorder) and preorder[i] < root.val: i += 1 root.left = build_tree(preorder[1:i]) root.right = build_tree(preorder[i:]) return root def count_leaf(root): if not root: return 0 if not root.left and not root.right: return 1 return count_leaf(root.left) + count_leaf(root.right) def max_depth(root): if not root: return 0 return max(max_depth(root.left), max_depth(root.right)) + 1 preorder = [5, 3, 1, 4, 7, 6, 8] root = build_tree(preorder) print(count_leaf(root)) # 4 print(max_depth(root)) # 3 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值