```
public int countNodes(TreeNode root) {
if (root == null) {
return 0;
}
if(root.left==null && root.right==null){
return 1;
}
double height = count(root);
int sum = 0;
while(height>=0)
{
sum=sum+ (int)Math.pow(2, height);
}
return sum;
}
public static int count(TreeNode root) {
if (root == null) {
return 0;
}
int rightCount = count(root.right);
return rightCount+1;
}
```