270.Closest-Binary-Search-Tree-Value

270. Closest Binary Search Tree Value

题目地址

https://leetcode.com/problems/closest-binary-search-tree-value/

题目描述

Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.

Note:

Given target value is a floating point.
You are guaranteed to have only one unique value in the BST that is closest to the target.
Example:

Input: root = [4,2,5,1,3], target = 3.714286

    4
   / \
  2   5
 / \
1   3

Output: 4

代码

Approach #1 Recursive Inorder O(n)

Approach #2 Iterative Inorder

Last updated

Was this helpful?