Like most problems, I recommend solving this by hand and pay attention to the information you keep track of to solve it. Here you need to recognize how to get a sorted array from a binary search tree.