8000 20201016 · BoscoSuen/leetcode@c30c1c8 · GitHub
[go: up one dir, main page]

Skip to content

Commit c30c1c8

Browse files
committed
20201016
1 parent 807695f commit c30c1c8

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @lc app=leetcode id=653 lang=java
3+
*
4+
* [653] Two Sum IV - Input is a BST
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* int val;
12+
* TreeNode left;
13+
* TreeNode right;
14+
* TreeNode() {}
15+
* TreeNode(int val) { this.val = val; }
16+
* TreeNode(int val, TreeNode left, TreeNode right) {
17+
* this.val = val;
18+
* this.left = left;
19+
* this.right = right;
20+
* }
21+
* }
22+
*/
23+
class Solution {
24+
public boolean findTarget(TreeNode root, int k) {
25+
if (root == null) return false;
26+
Set<Integer> set = new HashSet<>();
27+
return dfs(root, set, k);
28+
}
29+
30+
private boolean dfs(TreeNode root, Set<Integer> set, int k) {
31+
if (root == null) return false;
32+
if (set.contains(k - root.val)) {
33+
return true;
34+
}
35+
set.add(root.val);
36+
return dfs(root.left, set, k) || dfs(root.right, set, k);
37+
}
38+
}
39+
// @lc code=end
40+

0 commit comments

Comments
 (0)
0