求二叉树中第K层结点的个数
一,问题描述
构建一棵二叉树(不一定是二叉查找树),求出该二叉树中第K层中的结点个数(根结点为第0层)
二,二叉树的构建
定义一个BinaryTree类来表示二叉树,二叉树BinaryTree 又是由各个结点组成的,因此需要定义一个结点类BinaryNode,BinaryNode作为BinaryTree的内部类。
此外,在BinaryTree中需要一定一个BinaryNode属性来表示树的根结点。
public class BinaryTree<T extends Comparable<? super T>> {
private static class BinaryNode<T>{
T element;
BinaryNode<T> left;
BinaryNode<T> right;
public BinaryNode(T element) {
this.element = element;
left = right = null;
}
public BinaryNode(T element, BinaryNode<T> left, BinaryNode<T> right){
this.element = element;
this.left = left;
this.right = right;
}
}
private BinaryNode<T> root;
//other code.....
第一行是二叉树类的定义,第三行是结点类的定义,第20行是二叉树根的定义。
三,求解第K层结点个数的算法实现
感觉对二叉树中的许多操作都可以用递归来实现。因此,二叉树是理解递归一个好实例。比如,二叉树的操作之统计二叉树中节点的个数 和 二叉树的先序遍历和后序遍历的应用--输出文件和统计目录大小
求第K层结点的个数也可以用递归来实现:
①若二叉树为空或者K小于0,返回0
②若K等于0,第0层就是树根,根只有一个,返回1
③若K大于0,返回左子树中第K-1层结点个数 加上 右子树中第K-1层结点的个数
因为,第K层结点,相对于根的左子树 和 右子树 而言,就是第K-1层结点
其实,这是有改进的地方:对于K<0的情形,准确地说:它只是一个非法输入,而不是递归的结束条件(基准条件)。可以看出,①不要把非法输入与递归的基准条件混淆,②把非法输入的判断放到递归中判断的开销是很大的。因为每进行一次递归就需要进行一次非法输入判断。而如果在开始就把非法输入过滤掉,在递归过程中就不会存在每一次递归就判断一次非法输入了。
递归的基准条件只有两个:
1) k==0 当递归到K==0时,说明:第K层是有结点的
2) root==null 当递归到root==null时,说明:第K层没有结点
因此,可以进一步将代码改进如下:这样,不需要在每次递归的过程中还可能附加一次 k<0 的判断
/**
*
* @param k
* @return 二叉树中第K层结点的个数(根位于第0层)
*/
public int k_nodes(int k){
if(k < 0)
return 0;
return k_nodes(root, k);
}
private int k_nodes(BinaryNode<T> root, int k){
if(root == null)
return 0;
if(k == 0)
return 1;//根结点
else
return k_nodes(root.left, k-1) + k_nodes(root.right, k-1);
}
可参考:按层打印二叉树--每行打印一层 来测试每一层是否有正确的结点个数。
四,代码实现
public class BinaryTree<T extends Comparable<? super T>> {
private static class BinaryNode<T>{
T element;
BinaryNode<T> left;
BinaryNode<T> right;
public BinaryNode(T element) {
this.element = element;
left = right = null;
}
}
private BinaryNode<T> root;
/**
* 向二叉树中插入一个元素
* @param element
*/
public void insert(T element){
root = insert(root, element);
}
private BinaryNode<T> insert(BinaryNode<T> root, T element){
if(root == null)
return new BinaryNode<T>(element);
int r = (int)(2*Math.random());
//随机地将元素插入到左子树 或者 右子树中
if(r==0)
root.left = insert(root.left, element);
else
root.right = insert(root.right, element);
return root;
}
/**
*
* @param k
* @return 二叉树中第K层结点的个数(根位于第0层)
*/
public int k_nodes(int k){
return k_nodes(root, k);
}
private int k_nodes(BinaryNode<T> root, int k){
if(root == null || k < 0)
return 0;
if(k == 0)
return 1;//根结点
else
return k_nodes(root.left, k-1) + k_nodes(root.right, k-1);
}
public static void main(String[] args) {
BinaryTree<Integer> tree = new BinaryTree<>();
int[] ele = C2_2_8.algorithm1(4);//构造一个随机数组,数组元素的范围为[1,4]
for (int i = 0; i < ele.length; i++) {
tree.insert(ele[i]);
}
int k_nodes = tree.k_nodes(2);//第二层
int k_nodes2 = tree.k_nodes(-1);//第-1层
int k_nodes3 = tree.k_nodes(0);
int k_nodes4 = tree.k_nodes(1);
int k_nodes5 = tree.k_nodes(4);//若超过了树的高度,结果为0
System.out.println(k_nodes);
System.out.println(k_nodes2);
System.out.println(k_nodes3);
System.out.println(k_nodes4);
System.out.println(k_nodes5);
}
}
关于 C2_2_8类,参考:随机序列生成算法---生成前N个整数的一组随机序列
五,参考资料
http://blog.csdn.net/luckyxiaoqiang/article/details/7518888
求二叉树中第K层结点的个数的更多相关文章
- 二叉树(9)----打印二叉树中第K层的第M个节点,非递归算法
1.二叉树定义: typedef struct BTreeNodeElement_t_ { void *data; } BTreeNodeElement_t; typedef struct BTree ...
- 六:二叉树中第k层节点个数与二叉树叶子节点个数
二叉树中第k层节点个数 递归解法: (1)假设二叉树为空或者k<1返回0 (2)假设二叉树不为空而且k==1.返回1 (3)假设二叉树不为空且k>1,返回左子树中k-1层的节点个数与右子树 ...
- 求二叉树第K层的节点个数+求二叉树叶子节点的个数
size_t _FindLeafSize(Node* root) //求二叉树叶子节点的个数 { //static size_t count = 0; if ...
- LeetCode OJ:Kth Smallest Element in a BST(二叉树中第k个最小的元素)
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- [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 ...
- 求数列中第K大的数
原创 利用到快速排序的思想,快速排序思想:https://www.cnblogs.com/chiweiming/p/9188984.html array代表存放数列的数组,K代表第K大的数,mid代表 ...
- [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 ...
- 求n!中因子k的个数
思路: 求n的阶乘某个因子k的个数,如果n比较小,可以直接算出来,但是如果n很大,此时n!超出了数据的表示范围,这种直接求的方法肯定行不通.其实n!可以表示成统一的方式. n!=(km)*(m!)*a ...
- hdu4587 Two Nodes 求图中删除两个结点剩余的连通分量的数量
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4587 题目给了12000ms,对于tarjan这种O(|V|+|E|)复杂度的算法来说,暴力是能狗住的 ...
随机推荐
- linux下SpringBoot Jar包自启脚本配置
今天整理服务器上SpringBoot项目发现是自启的,于是想看看实现.翻看离职同事的交接文档发现一个***.service文件内容如下 [Unit] Description=sgfront After ...
- Azkaban集群部署
一.部署模式 solo-server模式 (使用内置h2存储元数据): two-server模式 (1个webServer,1个execServer在同一服务器上,使用mysql存储元数据): mu ...
- sqlserver-表分区
最近对公司数据库性能方面改造.现已初建成效. 公司原先数据库问题颇多,简单列举下: 1.数据表文档缺失. 2.数据库900多张表,接近一半都是备份和一些报表,没有分库处理 3.大数量的表按照年份人工导 ...
- PAT题解-1118. Birds in Forest (25)-(并查集模板题)
如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...
- 一个java实现的简单的4则运算器
有些基础知识有欠缺,补一下,顺便练习一下java import com.sun.deploy.util.ArrayUtil; import java.util.*; public class Main ...
- web框架-Struts开始
问题: 为什么有structs 作为一种框架(frameset)可以与传统的mvc进行比较? MVC是一种模式数据处理.显示和数据输入分开,来规范开发,但是却又并不规范.可以这样想:有三家公司,他们对 ...
- 软工团队(hello world)组员介绍
姜中希:喜欢编程,善于交际. 周盼超:喜欢编程. 王昭博:思路比较清晰,可以明确开发步骤. 刘洪阳:思想笔记广泛,可以把很多有关联的东西结合起来. 刘双勃:喜欢编程,踏实,比较容易坚持.
- final发布48小时用户调查报告
小组名称:飞天小女警 项目名称:礼物挑选小工具 小组成员:沈柏杉(组长).程媛媛.杨钰宁.谭力铭 调查问卷标题:用户调查报告 调查目的:在final版本发布后的用户调查报告 调查问卷的数量:11 问卷 ...
- PAT 甲级 1090 Highest Price in Supply Chain
https://pintia.cn/problem-sets/994805342720868352/problems/994805376476626944 A supply chain is a ne ...
- Linux命令(四)删除文件 rm
用户可以使用 rm 命令删除不需要的文件. rm 可以删除文件或目录,并且支持通配符. 如果目录中存在其它文件则会递归删除. 删除软链接只是删除链接,对应的文件或目录不会被删除. 软链接类似于 win ...