Skip to content

Commit 844f0c2

Browse files
refactor 663
1 parent 79c5614 commit 844f0c2

File tree

2 files changed

+41
-39
lines changed

2 files changed

+41
-39
lines changed

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

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -52,42 +52,44 @@
5252
5353
*/
5454
public class _663 {
55-
/**
56-
* The idea is that we use a map to store the sum of each node, then in the end,
57-
* we check if any node has a sum that is exactly half of total sum.
58-
*/
59-
public boolean checkEqualTree(TreeNode root) {
60-
Map<TreeNode, Integer> map = new HashMap<>();
61-
int totalSum = sumForEachNode(root, map);
62-
if (totalSum % 2 != 0 || map.size() < 2) {
63-
return false;
64-
}
65-
for (TreeNode key : map.keySet()) {
66-
if (map.get(key) == totalSum / 2) {
67-
return true;
55+
public static class Solution1 {
56+
/**
57+
* The idea is that we use a map to store the sum of each node, then in the end,
58+
* we check if any node has a sum that is exactly half of total sum.
59+
*/
60+
public boolean checkEqualTree(TreeNode root) {
61+
Map<TreeNode, Integer> map = new HashMap<>();
62+
int totalSum = sumForEachNode(root, map);
63+
if (totalSum % 2 != 0 || map.size() < 2) {
64+
return false;
65+
}
66+
for (TreeNode key : map.keySet()) {
67+
if (map.get(key) == totalSum / 2) {
68+
return true;
69+
}
6870
}
71+
return false;
6972
}
70-
return false;
71-
}
7273

73-
private int sumForEachNode(TreeNode root, Map<TreeNode, Integer> map) {
74-
if (root == null) {
75-
return 0;
76-
}
77-
if (root.left == null && root.right == null) {
78-
map.put(root, root.val);
79-
return root.val;
80-
}
81-
int leftVal = 0;
82-
if (root.left != null) {
83-
leftVal = sumForEachNode(root.left, map);
84-
}
85-
int rightVal = 0;
86-
if (root.right != null) {
87-
rightVal = sumForEachNode(root.right, map);
74+
private int sumForEachNode(TreeNode root, Map<TreeNode, Integer> map) {
75+
if (root == null) {
76+
return 0;
77+
}
78+
if (root.left == null && root.right == null) {
79+
map.put(root, root.val);
80+
return root.val;
81+
}
82+
int leftVal = 0;
83+
if (root.left != null) {
84+
leftVal = sumForEachNode(root.left, map);
85+
}
86+
int rightVal = 0;
87+
if (root.right != null) {
88+
rightVal = sumForEachNode(root.right, map);
89+
}
90+
int val = root.val + leftVal + rightVal;
91+
map.put(root, val);
92+
return val;
8893
}
89-
int val = root.val + leftVal + rightVal;
90-
map.put(root, val);
91-
return val;
9294
}
9395
}

src/test/java/com/fishercoder/_663Test.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,45 @@
1111
import static org.junit.Assert.assertEquals;
1212

1313
public class _663Test {
14-
private static _663 test;
14+
private static _663.Solution1 solution1;
1515
private static TreeNode root;
1616
private static boolean expected;
1717

1818
@BeforeClass
1919
public static void setup() {
20-
test = new _663();
20+
solution1 = new _663.Solution1();
2121
}
2222

2323
@Test
2424
public void test1() {
2525
root = TreeUtils.constructBinaryTree(Arrays.asList(5, 10, 10, null, null, 2, 3));
2626
TreeUtils.printBinaryTree(root);
2727
expected = true;
28-
assertEquals(expected, test.checkEqualTree(root));
28+
assertEquals(expected, solution1.checkEqualTree(root));
2929
}
3030

3131
@Test
3232
public void test2() {
3333
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 10, null, null, 2, 20));
3434
TreeUtils.printBinaryTree(root);
3535
expected = false;
36-
assertEquals(expected, test.checkEqualTree(root));
36+
assertEquals(expected, solution1.checkEqualTree(root));
3737
}
3838

3939
@Test
4040
public void test3() {
4141
root = TreeUtils.constructBinaryTree(Arrays.asList(1, null, 2, 2));
4242
TreeUtils.printBinaryTree(root);
4343
expected = false;
44-
assertEquals(expected, test.checkEqualTree(root));
44+
assertEquals(expected, solution1.checkEqualTree(root));
4545
}
4646

4747
@Test
4848
public void test4() {
4949
root = TreeUtils.constructBinaryTree(Arrays.asList(0));
5050
TreeUtils.printBinaryTree(root);
5151
expected = false;
52-
assertEquals(expected, test.checkEqualTree(root));
52+
assertEquals(expected, solution1.checkEqualTree(root));
5353
}
5454

5555
}

0 commit comments

Comments
 (0)