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

Commit b64e618

Browse files
edit 653
1 parent dae9971 commit b64e618

File tree

2 files changed

+78
-19
lines changed

2 files changed

+78
-19
lines changed

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

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

33
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.Notes;
45

56
import java.util.ArrayList;
7+
import java.util.HashMap;
68
import java.util.List;
9+
import java.util.Map;
710

811
/**
912
* 653. Two Sum IV - Input is a BST
@@ -59,4 +62,35 @@ private void dfs(TreeNode root, List<Integer> list) {
5962
}
6063
}
6164
}
65+
66+
@Notes(todo = "This solution fails by _653Test.test6(), need to fix it.")
67+
public static class MapSolution {
68+
public boolean findTarget(TreeNode root, int k) {
69+
if (root == null) return false;
70+
Map<Integer, Integer> map = new HashMap();//value is index
71+
int index = 0;
72+
preorder(root, map, index);
73+
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
74+
if (map.containsKey(k - entry.getKey()) &&
75+
map.get(k - entry.getKey()) != entry.getValue()) {
76+
return true;
77+
}
78+
}
79+
return false;
80+
}
81+
82+
private void preorder(TreeNode root, Map<Integer, Integer> map, int index) {
83+
if (root == null) {
84+
return;
85+
}
86+
map.put(root.val, index++);
87+
if (root.left != null) {
88+
preorder(root.left, map, ++index);
89+
}
90+
if (root.right != null) {
91+
preorder(root.right, map, ++index);
92+
}
93+
}
94+
95+
}
6296
}
Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
package com.fishercoder;
22

33
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
45
import com.fishercoder.solutions._653;
56
import org.junit.Before;
67
import org.junit.BeforeClass;
78
import org.junit.Test;
89

10+
import java.util.ArrayList;
11+
import java.util.Arrays;
12+
913
import static junit.framework.Assert.assertEquals;
1014

1115
public class _653Test {
1216
private static _653.ListSolution listSolution;
17+
private static _653.MapSolution mapSolution;
1318
private static boolean expected;
1419
private static TreeNode root;
1520

1621
@BeforeClass
1722
public static void setup(){
1823
listSolution = new _653.ListSolution();
24+
mapSolution = new _653.MapSolution();
1925
}
2026

2127
@Before
@@ -25,51 +31,70 @@ public void setupForEachTest(){
2531

2632
@Test
2733
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+
root = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(5, 3, 2, 4, 6, 7)));
3435
expected = true;
3536
assertEquals(expected, listSolution.findTarget(root, 9));
37+
assertEquals(expected, mapSolution.findTarget(root, 9));
3638
}
3739

3840
@Test
3941
public void test2(){
40-
root = new TreeNode(2);
41-
root.left = new TreeNode(1);
42-
root.right = new TreeNode(3);
42+
root = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(2, 1, 3)));
4343
expected = true;
4444
assertEquals(expected, listSolution.findTarget(root, 4));
45+
assertEquals(expected, mapSolution.findTarget(root, 4));
4546
}
4647

4748
@Test
4849
public void test3(){
4950
root = new TreeNode(1);
5051
expected = false;
5152
assertEquals(expected, listSolution.findTarget(root, 2));
53+
assertEquals(expected, mapSolution.findTarget(root, 2));
5254
}
5355

5456
@Test
5557
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);
58+
/**
59+
* 2
60+
* / \
61+
* 0 3
62+
* / \
63+
* -4 1
64+
*
65+
* target = 1;
66+
* expected = true;
67+
* */
68+
69+
root = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(2, 0, -4, 1, 3)));
6170
expected = true;
6271
assertEquals(expected, listSolution.findTarget(root, -1));
72+
assertEquals(expected, mapSolution.findTarget(root, -1));
6373
}
6474

6575
@Test
6676
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);
77+
root = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(2, 1, 3, -4, 0)));
7278
expected = true;
7379
assertEquals(expected, listSolution.findTarget(root, 2));
80+
assertEquals(expected, mapSolution.findTarget(root, 2));
81+
}
82+
83+
@Test
84+
public void test6(){
85+
root = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(
86+
3393, 2264, 4972, 1908, 3252, 4128, 5140, 965, 2018,
87+
3082, null, 3838, 4196, 5085, null, 559, 1187, null, 2143, 2968,
88+
null, 3810, 3957, null, 4825, null, null, 0, 908, 1135, 1659, null,
89+
null, 2764, null, 3581, null, null, 4106, 4498, null, null,
90+
498, 821, null, null, null, 1262, 1826, 2513, 2910, 3486, 3708,
91+
null, null, 4377, 4673, 231, null, null, null, null, 1482,
92+
null, null, 2386, 2690, null, null, null, null, null, null, 4349, null,
93+
null, null, 170, 376, 1421, 1613, null, null, 2534, null,
94+
null, null, 96, null, null, null, 1303)));
95+
expected = true;
96+
assertEquals(expected, listSolution.findTarget(root, 5831));
97+
TreeUtils.printBinaryTree(root);
98+
// assertEquals(expected, mapSolution.findTarget(root, 5831));
7499
}
75100
}

0 commit comments

Comments
 (0)