力扣222 代码随想录Day15 第四题

完全二叉树结点的数量

cpp 复制代码
class Solution {
public:
    int countNodes(TreeNode* root) {
        if(root==NULL) return 0;
        TreeNode* le=root->left;
        TreeNode* ri=root->right;
        int ld=0;
        int rd=0;
        while(le){
            le=le->left;
            ld++;
        }
        while(ri){
            ri=ri->right;
            rd++;
        }
        if(ld==rd) return(2<<ld)-1;
        int ln=countNodes(root->left);
        int rn=countNodes(root->right);
        int result=ln+rn+1;
        return result;
    }
};
相关推荐
政企项目老覃6 小时前
大模型幻觉治理与自动评测:金融风控场景的落地实践
人工智能·算法·机器学习
淡海水6 小时前
08-03-不可变-ImmutableDictionary-TKey-TValue-与ImmutableHashSet-T-持久化哈希树
数据结构·算法·c#·哈希算法·dictionary·immutable
hansang_IR6 小时前
【题解】[APIO2023] 赛博乐园 / cyberland
c++·算法·图论
洛阳纸贵6 小时前
MATLAB-matlab基础知识
学习·算法·matlab
落羽的落羽7 小时前
【AI】快速理解AI应用的相关名词概念
linux·c++·人工智能·python·计算机网络·算法
Nil2087 小时前
leetcode 17电话号码的字母组合
算法·leetcode·职场和发展
203号居民8 小时前
LeetCode hot 100 — 25. K 个一组翻转链表
算法·leetcode·链表
ocean21039 小时前
2025-2026年AI算法与模型研发面试高频知识点洞察
人工智能·算法·面试
Nil2089 小时前
leetcode 78子集
数据结构·算法·leetcode