/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int get_sum(TreeNode *cur)
{
if(cur == 0)return 0;
int g_left = get_sum(cur ->left);
int g_right = get_sum(cur->right);
int sum = 1 + g_left + g_right;
return sum;
}
int countNodes(TreeNode* root) {
return get_sum(root);
}
};
222. 完全二叉树的节点个数
最新推荐文章于 2024-08-29 22:25:44 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)