LeetCode 0270. Closest Binary Search Tree Value Solution in Java, Python, C++, JavaScript, Go & Rust | Explanation + Code

CoderIndeed
0
0270. Closest Binary Search Tree Value

Description

Given the root of a binary search tree and a target value, return the value in the BST that is closest to the target. If there are multiple answers, print the smallest.

 

Example 1:

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

Example 2:

Input: root = [1], target = 4.428571
Output: 1

 

Constraints:

  • The number of nodes in the tree is in the range [1, 104].
  • 0 <= Node.val <= 109
  • -109 <= target <= 109

Solutions

Solution 1: Recursion

We define a recursive function dfs(node), which starts from the current node node and finds the node closest to the target value target. We can update the answer by comparing the absolute difference between the current node's value and the target value. If the target value is less than the current node's value, we recursively search the left subtree; otherwise, we recursively search the right subtree.

The time complexity is O(n), and the space complexity is O(n). Where n is the number of nodes in the binary search tree.

PythonJavaC++GoTypeScriptJavaScript
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def closestValue(self, root: Optional[TreeNode], target: float) -> int: def dfs(node: Optional[TreeNode]): if node is None: return nxt = abs(target - node.val) nonlocal ans, diff if nxt < diff or (nxt == diff and node.val < ans): diff = nxt ans = node.val node = node.left if target < node.val else node.right dfs(node) ans = 0 diff = inf dfs(root) return ans(code-box)

Solution 2: Iteration

We can rewrite the recursive function in an iterative form, using a loop to simulate the recursive process. We start from the root node and check whether the absolute difference between the current node's value and the target value is less than the current minimum difference. If it is, we update the answer. Then, based on the size relationship between the target value and the current node's value, we decide to move to the left subtree or the right subtree. The loop ends when we traverse to a null node.

The time complexity is O(n), where n is the number of nodes in the binary search tree. The space complexity is O(1).

PythonJavaC++GoTypeScriptJavaScript
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def closestValue(self, root: Optional[TreeNode], target: float) -> int: ans, diff = root.val, inf while root: nxt = abs(root.val - target) if nxt < diff or (nxt == diff and root.val < ans): diff = nxt ans = root.val root = root.left if target < root.val else root.right return ans(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !