二叉树 (Binary Tree)相关题目(求总节点数 叶子节点数 深度 第k层节点数)
来源:互联网 发布:代码性能优化php 编辑:程序博客网 时间:2024/05/01 16:03
1.求二叉树中节点个数
2.求二叉树叶子节点个数
3.求二叉树的深度
4.求二叉树第K层的节点个数
#include <stdio.h>#include <stdlib.h>#define ElementType chartypedef struct Node { ElementType data; struct Node *lchild; struct Node *rchild;}BinaryTree;//创建二叉树节点BinaryTree* CreateBinaryTree(data) { BinaryTree* t = (BinaryTree*)malloc(sizeof(BinaryTree)); if (!t) { printf("空间不足!\n"); return NULL; } t->lchild = NULL; t->rchild = NULL; t->data = data; return t;}/* 求二叉树中节点个数 递归解法: (1)如果二叉树为空,节点个数为0 (2)如果二叉树不为空,二叉树节点个数 = 左子树节点个数 + 右子树节点个数 + 1*/int GetNodeNum(BinaryTree* BT) { if (BT == NULL) return 0; int leftNum = GetNodeNum(BT->lchild); //左子树节点个数 int rightNum = GetNodeNum(BT->rchild); //右子树节点个数 return (leftNum + rightNum + 1);}/* 求二叉树叶子节点个数 递归解法: (1)如果二叉树为空,返回0 (2)如果二叉树不为空且左右子树为空,返回1 (3)如果二叉树不为空,且左右子树不同时为空,返回左子树中叶子节点个数加上右子树中叶子节点个数*/int GetLeafNodeNum(BinaryTree* BT) { if (BT == NULL) return 0; if (BT->lchild == NULL && BT->rchild == NULL) return 1; int leftNum = GetLeafNodeNum(BT->lchild); //左子树中叶子节点个数 int rightNum = GetLeafNodeNum(BT->rchild); //右子树中叶子节点个数 return (leftNum + rightNum);}/* 求二叉树的深度 递归解法: (1)如果二叉树为空,二叉树的深度为0 (2)如果二叉树不为空,二叉树的深度 = max(左子树深度, 右子树深度) + 1*/int GetDepth(BinaryTree* BT) { if (BT == NULL) return 0; int leftDepth = GetDepth(BT->lchild); //左子树深度 int rightDepth = GetDepth(BT->rchild); //右子树深度 int maxDepth = (leftDepth > rightDepth) ? leftDepth : rightDepth; return (maxDepth + 1);}/* 求二叉树第K层的节点个数 递归解法: (1)如果二叉树为空或者k<1返回0 (2)如果二叉树不为空并且k==1,返回1 (3)如果二叉树不为空且k>1,返回左子树中k-1层的节点个数与右子树k-1层节点个数之和*/int GetNodeNumKthLevel(BinaryTree* BT, int k) { if (BT == NULL || k < 1) return 0; if (k == 1) return 1; int leftNum = GetNodeNumKthLevel(BT->lchild, k - 1); int rightNum = GetNodeNumKthLevel(BT->rchild, k - 1); return (leftNum + rightNum);}int main(int argc, const char * argv[]) { BinaryTree* tree_A = CreateBinaryTree('A'); BinaryTree* tree_B = CreateBinaryTree('B'); tree_A->lchild = tree_B; BinaryTree* tree_C = CreateBinaryTree('C'); tree_A->rchild = tree_C; BinaryTree* tree_D = CreateBinaryTree('D'); tree_B->lchild = tree_D; BinaryTree* tree_F = CreateBinaryTree('F'); tree_B->rchild = tree_F; BinaryTree* tree_E = CreateBinaryTree('E'); tree_F->lchild = tree_E; BinaryTree* tree_G = CreateBinaryTree('G'); tree_C->lchild = tree_G; BinaryTree* tree_I = CreateBinaryTree('I'); tree_C->rchild = tree_I; BinaryTree* tree_H = CreateBinaryTree('H'); tree_G->rchild = tree_H; int num = GetNodeNum(tree_A); printf("二叉树节点个数:%d", num); int depth = GetDepth(tree_A); printf("\n二叉树深度:%d", depth); int k = 3; int kthNum = GetNodeNumKthLevel(tree_A, k); printf("\n二叉树第%d层节点数:%d", k, kthNum); int leafNum = GetLeafNodeNum(tree_A); printf("\n二叉树叶子节点个数:%d", leafNum); return 0;}
0 0
- 二叉树 (Binary Tree)相关题目(求总节点数 叶子节点数 深度 第k层节点数)
- 二叉树第k层的叶子节点数
- 求二叉树第k 层的节点数
- 求二叉树非叶子节点数
- 二叉树(8)----求二叉树第K层的节点数和二叉树第K层的叶子节点数,递归方式和非递归方式
- 二叉树的递归实现(前序,中序,后序,层序,高度,叶子节点数,第k层节点数)
- 求二叉树的节点个数、叶子节点个数、第k层的节点个数
- 计算二叉树的高度、节点数、叶子节点数
- 二叉树的创建、遍历、深度、叶子节点数
- 二叉树的深度和叶子节点数
- 数据结构树求深度和叶子节点数
- 递归遍历 二叉树 求高度 和 节点数 和 叶子节点数
- 二叉树的字符串创建和遍历,求深度,叶子节点数
- 二叉树求深度和宽度,叶子节点数,总结点数
- 叶子节点数
- 已知二叉树的节点数,求树的深度
- 求二叉树第K层的叶子节点的个数(假设根节点是第一层)
- 二叉树的节点数和二叉树的叶子节点书和二叉树的深度
- Android记录25-WebView实现离线缓存阅读
- UVALive - 5103 Computer Virus on Planet Pandora Description
- Oracle存储过程完整结构讲解
- iOS sqlite3 使用
- 在将String写入txt文件时的换行问题
- 二叉树 (Binary Tree)相关题目(求总节点数 叶子节点数 深度 第k层节点数)
- 笔试面试错题集2
- zookeeper集群的配置
- sigaction函数说明
- HDU1175 连连看
- G-Queue(CF-545D
- LA 5902 Movie collection (树状数组)
- 【自考学习】数据库系统原理总结
- php 验证表单是否输入