Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Try pre-computing the answer for each node from 1 to n, and answer each query in O(1).
The answers can be precomputed in a single tree traversal after computing the height of each subtree.