Skip to content

Commit ea2de74

Browse files
add a solution for 653
1 parent 1d77174 commit ea2de74

File tree

1 file changed

+20
-0
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+20
-0
lines changed

src/main/java/com/fishercoder/solutions/_653.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import com.fishercoder.common.classes.TreeNode;
44

55
import java.util.ArrayList;
6+
import java.util.HashSet;
67
import java.util.List;
8+
import java.util.Set;
79

810
public class _653 {
911

@@ -34,4 +36,22 @@ private void dfs(TreeNode root, List<Integer> list) {
3436
}
3537
}
3638
}
39+
40+
public static class Solution2 {
41+
public boolean findTarget(TreeNode root, int k) {
42+
return dfs(root, new HashSet(), k);
43+
}
44+
45+
private boolean dfs(TreeNode root, Set<Integer> set, int k) {
46+
if (root == null) {
47+
return false;
48+
}
49+
50+
if (set.contains(k - root.val)) {
51+
return true;
52+
}
53+
set.add(root.val);
54+
return dfs(root.left, set, k) || dfs(root.right, set, k);
55+
}
56+
}
3757
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy