Skip to content

Commit 18a93dd

Browse files
committed
Add solution #653
1 parent 3119a42 commit 18a93dd

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
617|[Merge Two Binary Trees](./0617-merge-two-binary-trees.js)|Easy|
8888
628|[Maximum Product of Three Numbers](./0628-maximum-product-of-three-numbers.js)|Easy|
8989
648|[Replace Words](./0648-replace-words.js)|Medium|
90-
653|[Two Sum IV - Input is a BST](./0653-two-sum-iv---input-is-a-bst.js)|Easy|
90+
653|[Two Sum IV - Input is a BST](./0653-two-sum-iv-input-is-a-bst.js)|Easy|
9191
686|[Repeated String Match](./0686-repeated-string-match.js)|Easy|
9292
700|[Search in a Binary Search Tree](./0700-search-in-a-binary-search-tree.js)|Easy|
9393
701|[Insert into a Binary Search Tree](./0701-insert-into-a-binary-search-tree.js)|Medium|
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 true
7+
* if there exist two elements in the BST such that their sum is equal to the
8+
* 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+
}

0 commit comments

Comments
 (0)