Tree: Depth-First Search - Count Complete Tree Nodes
The following code attempts to count nodes in a complete binary tree using the optimal approach. Identify the subtle bug that causes incorrect counts on some inputs.
```python
def exists(idx, h, root):
left, right = 0, 2**(h - 1) - 1
for _ in range(h - 1):
mid = (left + right) // 2
if idx < mid:
root = root.left
right = mid
else:
root = root.right
left = mid + 1
return root is not None
```
