题目:
题解:
bool exists(struct TreeNode* root, int level, int k) {
int bits = 1 << (level - 1);
struct TreeNode* node = root;
while (node != NULL && bits > 0) {
if (!(bits & k)) {
node = node->left;
} else {
node = node->right;
}
bits >>= 1;
}
return node != NULL;
}
int countNodes(struct TreeNode* root) {
if (root == NULL) {
return 0;
}
int level = 0;
struct TreeNode* node = root;
while (node->left != NULL) {
level++;
node = node->left;
}
int low = 1 << level, high = (1 << (level + 1)) - 1;
while (low < high) {
int mid = (high - low + 1) / 2 + low;
if (exists(root, level, mid)) {
low = mid;
} else {
high = mid - 1;
}
}
return low;
}