```
public class Solution
{
public int countNodes(TreeNode root)
{
if(root==null)
return 0;
int left = count(root.left);
int right = count(root.right);
if(left<=right)
{
return ((int)(Math.pow(2, left)-1) + countNodes(root.right) + 1);
}
else
{
return (countNodes(root.left) + (int)(Math.pow(2, right)-1) + 1);
}
}
public static int count(TreeNode root)
{
int ctr=0;
while(root!=null)
{
ctr++;
root = root.left;
}
return ctr;
}
}
```