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