bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 270. Closest Binary Search Tree Value

270. Closest Binary Search Tree Value

Leetcode Solutions

Approach: Binary Search

  1. Initialize a variable closest to store the closest value found so far.
  2. Start with the root node and compare its value with the target.
  3. If the target is equal to the current node's value, return this value as it is the closest possible.
  4. Update closest with the current node's value if it is closer to the target than the previous closest.
  5. Decide whether to go left or right:
    • If the target is less than the current node's value, go left.
    • If the target is greater than the current node's value, go right.
  6. If the current node is null, meaning we've reached a leaf, return closest.
  7. Repeat steps 2-6 until the closest value is found.
UML Thumbnail

Approach: Recursive Inorder + Linear Search

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR