/**
 * struct TreeNode {
 *	int val;
 *	struct TreeNode *left;
 *	struct TreeNode *right;
 *	TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 * };
 */
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @return int整型
     */
    int countNodes(TreeNode* root) {
        // write code here
        if(!root)return 0;
        int l=countNodes(root->left);
        int r=countNodes(root->right);
        return l+r+1;
    }
};

一、题目考察的知识点

递归

二、题目解答方法的文字分析

边递归边加上点,把左子树右子树上的点数加起来最后加上根节点就是答案

三、本题解析所用的编程语言

c++