/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int countNodes(TreeNode* root) {
int left = findLeftDepth(root);
int right = findRightDepth(root);
if (left == right) {
return pow(2, left) - 1;
}
return countNodes(root->left) + countNodes(root->right) + 1;
}
private:
int findLeftDepth(TreeNode* root) {
if (!root) {
return 0;
}
return findLeftDepth(root->left) + 1;
}
int findRightDepth(TreeNode* root) {
if (!root) {
return 0;
}
return findRightDepth(root->right) + 1;
}
};