【完全二叉树节点数!】【深度优先】【广度优先】Leetcode 222 完全二叉树的节点个数

【完全二叉树】【深度优先】【广度优先】Leetcode 222 完全二叉树的节点个数

    • [:star:解法1 按照完全二叉树](#:star:解法1 按照完全二叉树)
    • [解法2 按照普通二叉树:深度优先遍历 后序 左右中](#解法2 按照普通二叉树:深度优先遍历 后序 左右中)
    • [解法3 按照普通二叉树:广度优先遍历 层序遍历](#解法3 按照普通二叉树:广度优先遍历 层序遍历)

---------------🎈🎈题目链接🎈🎈-------------------

⭐️解法1 按照完全二叉树

完全二叉树只有两种情况:

情况一:就是满二叉树,

情况二:最后一层叶子节点没有满。

对于情况一,可以直接用 2 ^ 树深度 - 1 来计算,注意这里根节点深度为1。

对于情况二,分别递归左孩子,和右孩子,递归到某一深度一定会有左孩子或者右孩子为满二叉树,然后依然可以按照情况1来计算。

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int countNodes(TreeNode root) {
        // 按照完全二叉树的特性 满二叉树的结点数为:2^depth - 1
        
        // 完全二叉树只有两种情况,情况一:就是满二叉树,情况二:最后一层叶子节点没有满
        // 如果是满二叉树,则节点个数为2^depth - 1
        // 对于情况二,分别递归左孩子,和右孩子,递归到某一深度一定会有左孩子或者右孩子为满二叉树,然后依然可以按照情况1来计算。
        if(root == null) return 0;
        TreeNode left = root.left;
        TreeNode right = root.right;
        int leftDepth = 1, rightDepth = 1;
        while(left != null){
            left = left.left;
            leftDepth ++;
        }
        while(right != null){
            right = right.right;
            rightDepth ++;
        }
        if(leftDepth == rightDepth){
            return (int)Math.pow(2,leftDepth) -1;
        }


        // 单层递归逻辑
        int leftnum = countNodes(root.left); //左
        int rightnum = countNodes(root.right);//右
        int result = leftnum + rightnum +1;  //中

        return result;
    }
}

解法2 按照普通二叉树:深度优先遍历 后序 左右中

/递归逻辑:左子树个数 右子树个数 加在一起返回给中间节点 +1

时间复杂度:O(n)

空间复杂度:O(log n),算上了递归系统栈占用的空间

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int countNodes(TreeNode root) {
        // 深度优先遍历 后序 左右中 
        
        if(root == null) return 0;  //终止条件
        
        // 递归逻辑:左子树个数 右子树个数 加在一起返回给中间节点 +1
        int leftnum = countNodes(root.left);
        int rightnum = countNodes(root.right);
        int result = leftnum+rightnum+1;
        return result;
         
    }
}       
    

解法3 按照普通二叉树:广度优先遍历 层序遍历

时间复杂度O(N)

空间复杂度O(N)

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int countNodes(TreeNode root) {
        // 层序遍历
        if(root == null) {
            return 0;
        }
        Queue<TreeNode> myqueue= new LinkedList<>();
        int result = 0;
        myqueue.add(root);
        while(!myqueue.isEmpty()){
            int size = myqueue.size();
            result += size;
            for(int i = 0; i<size;i++){
                TreeNode temp = myqueue.poll();
                if(temp.left != null){
                    myqueue.add(temp.left);
                }
                if(temp.right != null){
                    myqueue.add(temp.right);
                }
            }
        }
        return result;
    }
}
相关推荐
颜酱2 小时前
图的数据结构:从「多叉树」到存储与遍历
javascript·后端·算法
架构师沉默7 小时前
别又牛逼了!AI 写 Java 代码真的行吗?
java·后端·架构
zone77398 小时前
006:RAG 入门-面试官问你,RAG 为什么要切块?
后端·算法·面试
CoovallyAIHub10 小时前
OpenClaw 近 2000 个 Skills,为什么没有一个好用的视觉检测工具?
深度学习·算法·计算机视觉
CoovallyAIHub10 小时前
CVPR 2026 | 用一句话告诉 AI 分割什么——MedCLIPSeg 让医学图像分割不再需要海量标注
深度学习·算法·计算机视觉
CoovallyAIHub11 小时前
Claude Code 突然变成了 66 个专家?这个 5.8k Star 的开源项目,让我重新理解了什么叫"会用 AI"
深度学习·算法·计算机视觉
兆子龙11 小时前
前端哨兵模式(Sentinel Pattern):优雅实现无限滚动加载
前端·javascript·算法
后端AI实验室11 小时前
我把一个生产Bug的排查过程,交给AI处理——20分钟后我关掉了它
java·ai
凉年技术13 小时前
Java 实现企业微信扫码登录
java·企业微信
xlp666hub14 小时前
Leetcode第五题:用C++解决盛最多水的容器问题
linux·c++·leetcode