-
Notifications
You must be signed in to change notification settings - Fork 7
/
653-Two-Sum-IV-Input-is-a-BST.js
56 lines (47 loc) · 1.17 KB
/
653-Two-Sum-IV-Input-is-a-BST.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// 653. Two Sum IV - Input is a BST [Easy]
// https://leetcode.com/problems/two-sum-iv-input-is-a-bst/
/**
* 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} k
* @return {boolean}
*/
const findTarget = (root, k) => {
const nums = [];
const traverse = (root) => {
if (!root) return;
nums.push(root.val);
traverse(root.left);
traverse(root.right);
};
traverse(root);
return twoSum(nums, k);
};
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
function twoSum(nums, target) {
const arr = nums.map((el, idx) => [el, idx]).sort((a, b) => a[0] - b[0]);
let left = 0;
let right = nums.length - 1;
while (left < right) {
const sum = arr[left][0] + arr[right][0];
if (sum === target) {
return true;
} else if (sum < target) {
left++;
} else {
right--;
}
}
return false;
}