Choose the Closest Search-Tree Value with Smaller-Value Ties

Quick Overview

Choose the Closest Search-Tree Value with Smaller-Value Ties. Return the BST value nearest to the double target. If distances tie, return the smaller value. root is an original TreeNode* with val, left and right fields.

Choose the Closest Search-Tree Value with Smaller-Value Ties

Company: Roblox

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Return the BST value nearest to the double target. If distances tie, return the smaller value. root is an original TreeNode* with val, left and right fields. ```cpp int closestValue(TreeNode* root, double target); ``` ### Examples ```text root = [6,3,9], target = 4.5 -> 3 root = [6,3,9], target = 8.8 -> 9 ``` ### Constraints - The BST has 1 through 10000 nodes. - Values lie in [0,1000000000], target in [-1000000000,1000000000].

Overview: Choose the Closest Search-Tree Value with Smaller-Value Ties. Return the BST value nearest to the double target. If distances tie, return the smaller value. root is an original TreeNode* with val, left and right fields.

Roblox logo
Roblox
Sep 26, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return the BST value nearest to the double target. If distances tie, return the smaller value. root is an original TreeNode* with val, left and right fields.

int closestValue(TreeNode* root, double target);

Examples

root = [6,3,9], target = 4.5 -> 3
root = [6,3,9], target = 8.8 -> 9

Constraints

  • The BST has 1 through 10000 nodes.
  • Values lie in [0,1000000000], target in [-1000000000,1000000000].