خرید بک لینک

Vote count: 0

I encountered the following leetcode question and I had a question about the approach some people used to solve it. The question is: Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target.

Note: Given target value is a floating point.

You may assume k is always valid, that is: k ≤ total nodes.

You are guaranteed to have only one unique set of k values in the BST that are closest to the target.

So, what some people did is that they did an in order traversal while maintaining a k sized queue of the closest elements. During the in order traversal, if they find an element that is closer to the target than the first node in the queue, they remove the first node from the queue and add in the current value. My question is, why do they compare with the first element in the queue? Here is some code for what I am referring to: https://leetcode.com/discuss/94472/inorder-one-linkedlist-java-solution-beat-85%25

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: يکشنبه 23 خرداد 1395 ساعت: 9:55

صفحه بندی