int countNodes(TreeNode* root) {
     if(!root) return 0;
     
     int l = getLeftDepth(root);
     int r = getRightDepth(root);
     
     if(l == r) return (2<<(l-1)) - 1;
     
     return countNodes(root->left) + countNodes(root->right) + 1;
 }
示例#2
0
 int countNodes(TreeNode* root) {
     if(root == NULL) return 0;
     if(root->left == NULL && root->right == NULL) return 1;
     int l = getLeftDepth(root);
     int r = getRightDepth(root);
     if(l == r) {
         return (1<<l) - 1;
     } else {
         return 1 + countNodes(root->left) + countNodes(root->right);
     }
 }