From 580b6454dd3ba6e54cb12ada7a9981253c3ecd8e Mon Sep 17 00:00:00 2001 From: TK Date: Wed, 6 Dec 2023 07:55:54 -0300 Subject: [PATCH] * --- .../find-closest-value-in-bst/find-closest-value-in-bst.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/coding_interviews/algoexpert/find-closest-value-in-bst/find-closest-value-in-bst.js b/coding_interviews/algoexpert/find-closest-value-in-bst/find-closest-value-in-bst.js index 0c10ef8..9df3b7b 100644 --- a/coding_interviews/algoexpert/find-closest-value-in-bst/find-closest-value-in-bst.js +++ b/coding_interviews/algoexpert/find-closest-value-in-bst/find-closest-value-in-bst.js @@ -1,13 +1,13 @@ // Runtime: O(N), where N = number of nodes in the tree // Space: O(1) -function findClosestValueInBst(tree, target, closestValue = null) { +function findClosestValueInBst(tree, target, closestValue = Infinity) { if (!tree) { return closestValue; } let currentDiff = Math.abs(tree.value - target); - let diff = closestValue ? Math.abs(closestValue - target) : currentDiff; + let diff = Math.abs(closestValue - target); let leftValue = findClosestValueInBst( tree.left, target,