llllmz

导航

222. 完全二叉树的节点个数c

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
int countNodes(struct TreeNode* root) {
    if(!root) return 0;
    if(!root->left&&!root->right) return 1;
    return countNodes(root->left)+countNodes(root->right)+1;
}

结果:

posted on 2024-03-05 16:40  神奇的萝卜丝  阅读(11)  评论(0)    收藏  举报