LeetCode-222. 完全二叉树的节点个数
给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。
class Solution {
public:
int fun(TreeNode* root){
if(!root)return 0;
int leftCount = fun(root->left);
int rightCount = fun(root->right);
return 1+leftCount+rightCount;
}
int countNodes(TreeNode* root) {
//if(!root)return 0;
return fun(root);
}
};
执行结果:
通过
执行用时:
32 ms, 在所有 C++ 提交中击败了50.37%的用户
内存消耗:
30.2 MB, 在所有 C++ 提交中击败了34.13%的用户
通过测试用例:
18 / 18
本文介绍了如何使用C++实现LeetCode题目222,计算完全二叉树的节点个数。通过递归方法,代码简洁高效,展示了算法思路和执行结果。
950

被折叠的 条评论
为什么被折叠?



