Skip to content

Commit 7caccd0

Browse files
committed
Add solution #653
1 parent d08c055 commit 7caccd0

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

0653-two-sum-iv---input-is-a-bst.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 653. Two Sum IV - Input is a BST
3+
* https://leetcode.com/problems/two-sum-iv-input-is-a-bst/
4+
* Difficulty: Easy
5+
*
6+
* Given the root of a Binary Search Tree and a target number k, return
7+
* true if there exist two elements in the BST such that their sum is
8+
* equal to the given target.
9+
*/
10+
11+
/**
12+
* Definition for a binary tree node.
13+
* function TreeNode(val, left, right) {
14+
* this.val = (val===undefined ? 0 : val)
15+
* this.left = (left===undefined ? null : left)
16+
* this.right = (right===undefined ? null : right)
17+
* }
18+
*/
19+
/**
20+
* @param {TreeNode} root
21+
* @param {number} k
22+
* @return {boolean}
23+
*/
24+
var findTarget = function(root, k) {
25+
const set = new Set();
26+
return dfs(set, root, k);
27+
};
28+
29+
function dfs(set, node, k) {
30+
if (!node) return false;
31+
if (set.has(k - node.val)) return true;
32+
33+
set.add(node.val);
34+
35+
return dfs(set, node.left, k) || dfs(set, node.right, k);
36+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
617|[Merge Two Binary Trees](./0617-merge-two-binary-trees.js)|Easy|
8787
628|[Maximum Product of Three Numbers](./0628-maximum-product-of-three-numbers.js)|Easy|
8888
648|[Replace Words](./0648-replace-words.js)|Medium|
89+
653|[Two Sum IV - Input is a BST](./0653-two-sum-iv---input-is-a-bst.js)|Easy|
8990
686|[Repeated String Match](./0686-repeated-string-match.js)|Easy|
9091
713|[Subarray Product Less Than K](./0713-subarray-product-less-than-k.js)|Medium|
9192
722|[Remove Comments](./0722-remove-comments.js)|Medium|

0 commit comments

Comments
 (0)