-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0272-closest-binary-search-tree-value-ii.js
More file actions
41 lines (37 loc) · 1.08 KB
/
0272-closest-binary-search-tree-value-ii.js
File metadata and controls
41 lines (37 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* 272. Closest Binary Search Tree Value II
* https://leetcode.com/problems/closest-binary-search-tree-value-ii/
* Difficulty: Hard
*
* Given the root of a binary search tree, a target value, and an integer k, return the k values
* in the BST that are closest to the target. You may return the answer in any order.
*
* You are guaranteed to have only one unique set of k values in the BST that are closest to the
* target.
*/
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} target
* @param {number} k
* @return {number[]}
*/
var closestKValues = function(root, target, k) {
const values = [];
inOrder(root);
values.sort((a, b) => Math.abs(a - target) - Math.abs(b - target));
return values.slice(0, k);
function inOrder(node) {
if (!node) return;
inOrder(node.left);
values.push(node.val);
inOrder(node.right);
}
};