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

Commit 7b8fa7d

Browse files
add 663
1 parent 52631c8 commit 7b8fa7d

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed

README.md

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

2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
25+
|663|[Equal Tree Partition](https://leetcode.com/problems/equal-tree-partition/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_663.java) | O(n) | O(n) | Medium | Tree
2526
|661|[Image Smoother](https://leetcode.com/problems/image-smoother/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_661.java) | O(m*n) | O(1) | Easy | Array
2627
|660|[Remove 9](https://leetcode.com/problems/remove-9/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_660.java) | O(n) | O(1) | Hard | Math
2728
|659|[Split Array into Consecutive Subsequences](https://leetcode.com/problems/split-array-into-consecutive-subsequences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_659.java) | O(n) | O(n) | Medium | HashMap
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
/**
9+
* 663. Equal Tree Partition
10+
*
11+
* Given a binary tree with n nodes,
12+
* your task is to check if it's possible to partition the tree to two trees which have the equal sum of values
13+
* after removing exactly one edge on the original tree.
14+
15+
Example 1:
16+
Input:
17+
5
18+
/ \
19+
10 10
20+
/ \
21+
2 3
22+
23+
Output: True
24+
Explanation:
25+
5
26+
/
27+
10
28+
29+
Sum: 15
30+
31+
10
32+
/ \
33+
2 3
34+
35+
Sum: 15
36+
37+
38+
Example 2:
39+
Input:
40+
1
41+
/ \
42+
2 10
43+
/ \
44+
2 20
45+
46+
Output: False
47+
Explanation: You can't split the tree into two trees with equal sum after removing exactly one edge on the tree.
48+
49+
Note:
50+
The range of tree node value is in the range of [-100000, 100000].
51+
1 <= n <= 10000
52+
53+
*/
54+
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) return false;
63+
for (TreeNode key : map.keySet()) {
64+
if (map.get(key) == totalSum / 2) {
65+
return true;
66+
}
67+
}
68+
return false;
69+
}
70+
71+
private int sumForEachNode(TreeNode root, Map<TreeNode, Integer> map) {
72+
if (root == null) {
73+
return 0;
74+
}
75+
if (root.left == null && root.right == null) {
76+
map.put(root, root.val);
77+
return root.val;
78+
}
79+
int leftVal = 0;
80+
if (root.left != null) {
81+
leftVal = sumForEachNode(root.left, map);
82+
}
83+
int rightVal = 0;
84+
if (root.right != null) {
85+
rightVal = sumForEachNode(root.right, map);
86+
}
87+
int val = root.val + leftVal + rightVal;
88+
map.put(root, val);
89+
return val;
90+
}
91+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._663;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import java.util.Arrays;
10+
11+
import static org.junit.Assert.assertEquals;
12+
13+
public class _663Test {
14+
private static _663 test;
15+
private static TreeNode root;
16+
private static boolean expected;
17+
18+
@BeforeClass
19+
public static void setup(){
20+
test = new _663();
21+
}
22+
23+
@Test
24+
public void test1(){
25+
root = TreeUtils.constructBinaryTree(Arrays.asList(5,10,10,null, null, 2,3));
26+
TreeUtils.printBinaryTree(root);
27+
expected = true;
28+
assertEquals(expected, test.checkEqualTree(root));
29+
}
30+
31+
@Test
32+
public void test2(){
33+
root = TreeUtils.constructBinaryTree(Arrays.asList(1,2,10,null, null, 2,20));
34+
TreeUtils.printBinaryTree(root);
35+
expected = false;
36+
assertEquals(expected, test.checkEqualTree(root));
37+
}
38+
39+
@Test
40+
public void test3(){
41+
root = TreeUtils.constructBinaryTree(Arrays.asList(1,null, 2, 2));
42+
TreeUtils.printBinaryTree(root);
43+
expected = false;
44+
assertEquals(expected, test.checkEqualTree(root));
45+
}
46+
47+
@Test
48+
public void test4(){
49+
root = TreeUtils.constructBinaryTree(Arrays.asList(0));
50+
TreeUtils.printBinaryTree(root);
51+
expected = false;
52+
assertEquals(expected, test.checkEqualTree(root));
53+
}
54+
55+
}

0 commit comments

Comments
 (0)