Leetcode Problem 2476. Closest Nodes Queries in a Binary Search Tree

2476. Closest Nodes Queries in a Binary Search Tree

Leetcode Solutions

Inorder Traversal and Binary Search

  1. Perform an inorder traversal of the BST and store the elements in a sorted list.
  2. For each query value, perform a binary search on the sorted list to find the mini and maxi values.
  3. For mini, find the largest value smaller than or equal to the query value. If no such value exists, use -1.
  4. For maxi, find the smallest value greater than or equal to the query value. If no such value exists, use -1.
  5. Store the results in a 2D array and return it.
UML Thumbnail

Iterative Traversal with On-the-fly Binary Search

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...