Reputation: 1447
Recently, I encounter an algorithm problem:
Given a binary search tree and a key, How to find first M tree nodes which values are closest to that key?
My idea is to use In-order traversal to put each tree node value into a another array. And we then use binary search to find the most closest node X to that given key in this array. In the end, we start search from this node X to the left and right side to find the first M values are closest to that given key.
However, My idea require O(n) time and O(n) space. is there some better thought than my idea?
Upvotes: 0
Views: 136
Reputation: 49893
Upvotes: 0