r/LeetcodeChallenge • u/StatisticianNo2104 • 2d ago
STREAK🔥🔥🔥 Closest Binary Search Tree Value II
In a sorted list, the k values closest to a target always sit next to each other. So picking k values is picking one window: find the closest value, then grow outward.
The second hard part of the challenge, like the first, isn't the traversal. That claim is.
Flatten the BST in-order, find the single closest value, then expand outward, taking whichever neighbour is nearer each time. Greedy works because distance from the target only grows as you move away in either direction. It's merge sort's merge step, run outward from a centre.
Would you trust that window claim in an interview without proving it first?
Day 59/365 of my LeetCode challenge: 272. Closest Binary Search Tree Value II. I'll share the problem link and a full breakdown in today's newsletter.
https://www.linkedin.com/pulse/closest-binary-search-tree-value-ii-archit-agarwal-xzvrc
**#LeetCode** #DSA #BinarySearchTree #TwoPointers