Skip to content

Commit 48b128a

Browse files
refactor 653
1 parent e9a3f0f commit 48b128a

File tree

2 files changed

+9
-51
lines changed

2 files changed

+9
-51
lines changed

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

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package com.fishercoder.solutions;
22

33
import com.fishercoder.common.classes.TreeNode;
4-
import com.fishercoder.common.utils.Notes;
54

65
import java.util.ArrayList;
7-
import java.util.HashMap;
86
import java.util.List;
9-
import java.util.Map;
107

118
/**
129
* 653. Two Sum IV - Input is a BST
@@ -39,7 +36,7 @@
3936
*/
4037
public class _653 {
4138

42-
public static class ListSolution {
39+
public static class Solution1 {
4340
public boolean findTarget(TreeNode root, int k) {
4441
if (root == null) {
4542
return false;
@@ -66,36 +63,4 @@ private void dfs(TreeNode root, List<Integer> list) {
6663
}
6764
}
6865
}
69-
70-
@Notes(todo = "This solution fails by _653Test.test6(), need to fix it.")
71-
public static class MapSolution {
72-
public boolean findTarget(TreeNode root, int k) {
73-
if (root == null) {
74-
return false;
75-
}
76-
Map<Integer, Integer> map = new HashMap();//value is index
77-
int index = 0;
78-
preorder(root, map, index);
79-
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
80-
if (map.containsKey(k - entry.getKey()) && map.get(k - entry.getKey()) != entry.getValue()) {
81-
return true;
82-
}
83-
}
84-
return false;
85-
}
86-
87-
private void preorder(TreeNode root, Map<Integer, Integer> map, int index) {
88-
if (root == null) {
89-
return;
90-
}
91-
map.put(root.val, index++);
92-
if (root.left != null) {
93-
preorder(root.left, map, ++index);
94-
}
95-
if (root.right != null) {
96-
preorder(root.right, map, ++index);
97-
}
98-
}
99-
100-
}
10166
}

src/test/java/com/fishercoder/_653Test.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@
1313
import static junit.framework.Assert.assertEquals;
1414

1515
public class _653Test {
16-
private static _653.ListSolution listSolution;
17-
private static _653.MapSolution mapSolution;
16+
private static _653.Solution1 solution1;
1817
private static boolean expected;
1918
private static TreeNode root;
2019

2120
@BeforeClass
2221
public static void setup() {
23-
listSolution = new _653.ListSolution();
24-
mapSolution = new _653.MapSolution();
22+
solution1 = new _653.Solution1();
2523
}
2624

2725
@Before
@@ -33,24 +31,21 @@ public void setupForEachTest() {
3331
public void test1() {
3432
root = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(5, 3, 2, 4, 6, 7)));
3533
expected = true;
36-
assertEquals(expected, listSolution.findTarget(root, 9));
37-
assertEquals(expected, mapSolution.findTarget(root, 9));
34+
assertEquals(expected, solution1.findTarget(root, 9));
3835
}
3936

4037
@Test
4138
public void test2() {
4239
root = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(2, 1, 3)));
4340
expected = true;
44-
assertEquals(expected, listSolution.findTarget(root, 4));
45-
assertEquals(expected, mapSolution.findTarget(root, 4));
41+
assertEquals(expected, solution1.findTarget(root, 4));
4642
}
4743

4844
@Test
4945
public void test3() {
5046
root = new TreeNode(1);
5147
expected = false;
52-
assertEquals(expected, listSolution.findTarget(root, 2));
53-
assertEquals(expected, mapSolution.findTarget(root, 2));
48+
assertEquals(expected, solution1.findTarget(root, 2));
5449
}
5550

5651
@Test
@@ -68,16 +63,14 @@ public void test4() {
6863

6964
root = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(2, 0, -4, 1, 3)));
7065
expected = true;
71-
assertEquals(expected, listSolution.findTarget(root, -1));
72-
assertEquals(expected, mapSolution.findTarget(root, -1));
66+
assertEquals(expected, solution1.findTarget(root, -1));
7367
}
7468

7569
@Test
7670
public void test5() {
7771
root = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(2, 1, 3, -4, 0)));
7872
expected = true;
79-
assertEquals(expected, listSolution.findTarget(root, 2));
80-
assertEquals(expected, mapSolution.findTarget(root, 2));
73+
assertEquals(expected, solution1.findTarget(root, 2));
8174
}
8275

8376
@Test
@@ -93,7 +86,7 @@ public void test6() {
9386
null, null, 170, 376, 1421, 1613, null, null, 2534, null,
9487
null, null, 96, null, null, null, 1303)));
9588
expected = true;
96-
assertEquals(expected, listSolution.findTarget(root, 5831));
89+
assertEquals(expected, solution1.findTarget(root, 5831));
9790
// TreeUtils.printBinaryTree(root);
9891
// assertEquals(expected, mapSolution.findTarget(root, 5831));
9992
}

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