Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit dc901e3

Browse files
add 653
1 parent 4183b92 commit dc901e3

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Your ideas/fixes/algorithms are more than welcome!
2020

2121
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2222
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
23+
|653|[Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_653.java) | | | Easy | Tree
2324
|652|[Find Duplicate Subtrees](https://leetcode.com/problems/find-duplicate-subtrees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_652.java) | O(n) |O(n) | Medium | Tree
2425
|651|[4 Keys Keyboard](https://leetcode.com/problems/4-keys-keyboard/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_651.java) | O(n^2) |O(n) | Medium | DP
2526
|650|[2 Keys Keyboard](https://leetcode.com/problems/2-keys-keyboard/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_650.java) | O(n^2) |O(n) | Medium | DP
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/**
9+
* 653. Two Sum IV - Input is a BST
10+
*
11+
* Given a Binary Search Tree and a target number,
12+
* return true if there exist two elements in the BST such that their sum is equal to the given target.
13+
14+
Example 1:
15+
Input:
16+
5
17+
/ \
18+
3 6
19+
/ \ \
20+
2 4 7
21+
22+
Target = 9
23+
Output: True
24+
25+
Example 2:
26+
Input:
27+
5
28+
/ \
29+
3 6
30+
/ \ \
31+
2 4 7
32+
33+
Target = 28
34+
Output: False
35+
36+
*/
37+
public class _653 {
38+
39+
public static class ListSolution {
40+
public boolean findTarget(TreeNode root, int k) {
41+
if (root == null) return false;
42+
List<Integer> list = new ArrayList<>();
43+
dfs(root, list);
44+
for (int i = 0; i < list.size() - 1; i++) {
45+
for (int j = i + 1; j < list.size(); j++) {
46+
if (list.get(i) + list.get(j) == k) return true;
47+
}
48+
}
49+
return false;
50+
}
51+
52+
private void dfs(TreeNode root, List<Integer> list) {
53+
list.add(root.val);
54+
if (root.left != null) {
55+
dfs(root.left, list);
56+
}
57+
if (root.right != null) {
58+
dfs(root.right, list);
59+
}
60+
}
61+
}
62+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.solutions._653;
5+
import org.junit.Before;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import static junit.framework.Assert.assertEquals;
10+
11+
public class _653Test {
12+
private static _653.ListSolution listSolution;
13+
private static boolean expected;
14+
private static TreeNode root;
15+
16+
@BeforeClass
17+
public static void setup(){
18+
listSolution = new _653.ListSolution();
19+
}
20+
21+
@Before
22+
public void setupForEachTest(){
23+
root = null;
24+
}
25+
26+
@Test
27+
public void test1(){
28+
root = new TreeNode(5);
29+
root.left = new TreeNode(3);
30+
root.left.left = new TreeNode(2);
31+
root.left.right = new TreeNode(4);
32+
root.right = new TreeNode(6);
33+
root.right.right = new TreeNode(7);
34+
expected = true;
35+
assertEquals(expected, listSolution.findTarget(root, 9));
36+
}
37+
38+
@Test
39+
public void test2(){
40+
root = new TreeNode(2);
41+
root.left = new TreeNode(1);
42+
root.right = new TreeNode(3);
43+
expected = true;
44+
assertEquals(expected, listSolution.findTarget(root, 4));
45+
}
46+
47+
@Test
48+
public void test3(){
49+
root = new TreeNode(1);
50+
expected = false;
51+
assertEquals(expected, listSolution.findTarget(root, 2));
52+
}
53+
54+
@Test
55+
public void test4(){
56+
root = new TreeNode(2);
57+
root.left = new TreeNode(0);
58+
root.left.left = new TreeNode(-4);
59+
root.left.right = new TreeNode(1);
60+
root.right = new TreeNode(3);
61+
expected = true;
62+
assertEquals(expected, listSolution.findTarget(root, -1));
63+
}
64+
65+
@Test
66+
public void test5(){
67+
TreeNode root = new TreeNode(2);
68+
root.left = new TreeNode(1);
69+
root.right = new TreeNode(3);
70+
root.left.left = new TreeNode(-4);
71+
root.left.left.right = new TreeNode(0);
72+
expected = true;
73+
assertEquals(expected, listSolution.findTarget(root, 2));
74+
}
75+
}

0 commit comments

Comments
 (0)