Skip to content

Commit 0a2aaa1

Browse files
authored
Update 270-closest-binary-search-tree-value.js
1 parent f420047 commit 0a2aaa1

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

270-closest-binary-search-tree-value.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ Output: 4
3535
* @param {number} target
3636
* @return {number}
3737
*/
38+
const closestValue = function(root, target) {
39+
let res = root.val
40+
while(root) {
41+
if(Math.abs(root.val - target) < Math.abs(res - target)) {
42+
res = root.val
43+
}
44+
root = root.val > target ? root.left : root.right
45+
}
46+
return res
47+
};
48+
49+
// another
50+
3851
const closestValue = function(root, target) {
3952
const child = target < root.val ? root.left : root.right;
4053
if (!child) return root.val;

0 commit comments

Comments
 (0)