0
votes

Is there any way to find the kth largest of all possible full path sums in a binary search tree without using any other storage, like array? Initially I thought that if I go on finding the sum of paths from the right while increasing a pointer, with each new sum being the minimum as that and the previous sum (initially sum is infinite), breaking out when the count reaches k. However I just discovered that while the maximum leaf values be naturally sorted from right to left, the sums need not be so. So this method won't work. How can I do this?

1

1 Answers

2
votes

If you can calculate the k-smallest then you can calculate the k-largest with the same algorithm. All you need to do is do things right-to-left instead of left-to-right.