/*
最底层的中间位置为根节点右儿子一直往左边遍历的点
我们每次判断这个点是否为空如果为空那就在左边否则在右边
*/
class Solution {
public:
int countNodes(TreeNode* root) {
if(!root) return 0;
int depth = f(root);
int ans=(1<<depth)- 1;
int tmp=1<<(depth-1);
for(int i=1;i<=depth;i++)
{
tmp/=2;
if(f(root->right)+i==depth) root=root->right;
else
{
ans-=tmp;
root=root->left;
}
}
return ans;
}
int f(TreeNode *root)
{
if(root == NULL) return 0;
return f(root->left)+1;
}
};