栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

多种方法实现统计完全二叉树的节点个数

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

多种方法实现统计完全二叉树的节点个数

222. 完全二叉树的节点个数

难度:中等

给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。

完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2^h 个节点。

思路

本文总共实现了三种方法

直接递归版本,没有利用到完全二叉树的特性迭代版本,没有利用到完全二叉树的特性递归版本,利用完全二叉树的特性进行计算 利用完全二叉树的特性进行计算的递归版本

对于完全二叉树中的一个节点,如果它的左子树的深度等于右子树的深度,那么该节点是一颗完全二叉树,可以使用公式 n u m = 2 depth − 1 num = 2^{text{depth}}-1 num=2depth−1直接计算该子树的节点个数,可以减少一部分的递归时间

public int countNodes(TreeNode root) {
    if (root == null) {
        return 0;
    }
    int leftDepth = 1;
    int rightDepth = 1;
    TreeNode leftNode = root.left;
    TreeNode rightNode = root.right;
    while (leftNode != null) {
        leftDepth++;
        leftNode = leftNode.left;
    }
    while (rightNode != null) {
        rightDepth++;
        rightNode = rightNode.right;
    }
    if (leftDepth == rightDepth) {
        return (int) Math.pow(2, leftDepth) - 1;
    }
    return countNodes(root.left) + countNodes(root.right) + 1;
}
直接递归版本
public int countNodes(TreeNode root) {
    if (root == null) {
        return 0;
    }

    return countNodes(root.left) + countNodes(root.right) + 1;
}
迭代版本
public int countNodes(TreeNode root) {
    if (root == null) {
        return 0;
    }
    int nodeNum = 0;
    Deque deque = new linkedList<>();
    deque.addLast(root);
    while (!deque.isEmpty()) {
        nodeNum++;
        TreeNode node = deque.removeFirst();
        if (node.left != null) {
            deque.addLast(node.left);
        }
        if (node.right != null) {
            deque.addLast(node.right);
        }
    }

    return nodeNum;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/732155.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号