一,问题描述

构建一棵二叉树(不一定是二叉查找树),求出该二叉树中第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层结点的个数的更多相关文章

  1. 二叉树(9)----打印二叉树中第K层的第M个节点,非递归算法

    1.二叉树定义: typedef struct BTreeNodeElement_t_ { void *data; } BTreeNodeElement_t; typedef struct BTree ...

  2. 六:二叉树中第k层节点个数与二叉树叶子节点个数

    二叉树中第k层节点个数 递归解法: (1)假设二叉树为空或者k<1返回0 (2)假设二叉树不为空而且k==1.返回1 (3)假设二叉树不为空且k>1,返回左子树中k-1层的节点个数与右子树 ...

  3. 求二叉树第K层的节点个数+求二叉树叶子节点的个数

    size_t _FindLeafSize(Node* root)     //求二叉树叶子节点的个数    {        //static size_t count = 0;        if ...

  4. 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 ...

  5. [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 ...

  6. 求数列中第K大的数

    原创 利用到快速排序的思想,快速排序思想:https://www.cnblogs.com/chiweiming/p/9188984.html array代表存放数列的数组,K代表第K大的数,mid代表 ...

  7. [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 ...

  8. 求n!中因子k的个数

    思路: 求n的阶乘某个因子k的个数,如果n比较小,可以直接算出来,但是如果n很大,此时n!超出了数据的表示范围,这种直接求的方法肯定行不通.其实n!可以表示成统一的方式. n!=(km)*(m!)*a ...

  9. hdu4587 Two Nodes 求图中删除两个结点剩余的连通分量的数量

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4587 题目给了12000ms,对于tarjan这种O(|V|+|E|)复杂度的算法来说,暴力是能狗住的 ...

随机推荐

  1. 《杜增强讲Unity之Tanks坦克大战》10-相机控制

    10 相机控制 本节主要学习如何控制相机自动调整位置和焦距,使两个坦克一直同时在视野内.   image 在Hierarchy点击右键   image 点击 Create Empty,生成空对象,改名 ...

  2. spring boot 2.0 源码分析(二)

    在上一章学习了spring boot 2.0启动的大概流程以后,今天我们来深挖一下SpringApplication实例变量的run函数. 先把这段run函数的代码贴出来: /** * Run the ...

  3. 关于InfiniBand几个基本知识点解释

    文章出处: https://blog.csdn.net/BtB5e6Nsu1g511Eg5XEg/article/details/83629279 公众号 https://blog.csdn.net/ ...

  4. PAT甲题题解-1104. Sum of Number Segments (20)-(水题)

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  5. PAT甲题题解-1124. Raffle for Weibo Followers-模拟,水题

    水题一个,贴个代码吧. #include <iostream> #include <cstdio> #include <algorithm> #include &l ...

  6. ElasticSearch 2 (16) - 深入搜索系列之近似度匹配

    ElasticSearch 2 (16) - 深入搜索系列之近似度匹配 摘要 标准的全文搜索使用TF/IDF处理文档.文档里的每个字段或一袋子词.match 查询可以告诉我们哪个袋子里面包含我们搜索的 ...

  7. 【Leetcode】81. Search in Rotated Sorted Array II

    Question: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? ...

  8. 关于vue-eslint自动补全代码,以及自动生成雪碧图

    一.配置eslint module.exports={ "printWidth": 240, //一行的字符数,如果超过会进行换行,默认为80 "tabWidth&quo ...

  9. [知乎]BAT占线

    黑色自有,蓝色全资收够,红色入股. https://www.zhihu.com/question/304396738/answer/547766603

  10. 深入解读Quartz的原理

     Quartz是一个大名鼎鼎的Java版开源定时调度器,功能强悍,使用方便.   一.核心概念   Quartz的原理不是很复杂,只要搞明白几个概念,然后知道如何去启动和关闭一个调度程序即可.   1 ...