http://bookshadow.com/weblog/2017/08/21/leetcode-equal-tree-partition/
这道题让我们划分等价树,就是说当移除一条边后,被分成的两棵树的结点之和需要相等。那么通过观察题目中的例子我们可以发现,如果我们将每个结点的结点值变成其所有子结点的结点值之和再加上当前的结点值,那么对于例子1来说,根结点的结点值就变成了30,断开位置的结点就变成了15,那么我们就可以发现其实只要断开位置的结点值是根结点值的一半,就存在等价划分。所以这道题的难点就是更新每个结点的结点值,我们可以使用递归来做。博主最开始使用的是unordered_set,把更新后的每个结点值都存入集合中,但是对于test case: [0, 1, -1] 会fail, 仔细分析下这个case,发现更新后的根结点值还是0,而且0已经被存入集合了,而0除以2还是0,在集合中存在,会返回true,但其实这棵树是不能等价划分的。0的情况确实比较特殊,所以我们使用unordered_map,建立更新后的结点值和其出现次数之间的映射,这样只有map中0的个数大于1的时候,才返回true。这样完美的避开了根结点为0的陷阱
https://discuss.leetcode.com/topic/100179/java-c-simple-solution-with-only-one-hashmap
X. https://blog.csdn.net/magicbean2/article/details/79183244
bool checkEqualTree(TreeNode* root) {
unordered_map<TreeNode*, int> hash; // node -> sum
int sum = getSum(root, hash);
if (sum % 2 != 0) {
return false;
}
return canSplit(root, hash, sum / 2);
}
private:
bool canSplit(TreeNode* root, unordered_map<TreeNode*, int> &hash, int half_sum) {
if (root == NULL) {
return false;
}
if (root->left && hash[root->left] == half_sum) {
return true;
}
if (root->right && hash[root->right] == half_sum) {
return true;
}
return canSplit(root->left, hash, half_sum) || canSplit(root->right, hash, half_sum);
}
int getSum(TreeNode* root, unordered_map<TreeNode*, int> &hash) {
if (root == NULL) {
return 0;
}
int left = getSum(root->left, hash);
int right = getSum(root->right, hash);
int sum = left + right + root->val;
hash[root] = sum;
return sum;
}
Given a binary tree with
n
nodes, your task is to check if it's possible to partition the tree to two trees which have the equal sum of values after removing exactly one edge on the original tree.
Example 1:
http://www.cnblogs.com/grandyang/p/7550360.html这道题让我们划分等价树,就是说当移除一条边后,被分成的两棵树的结点之和需要相等。那么通过观察题目中的例子我们可以发现,如果我们将每个结点的结点值变成其所有子结点的结点值之和再加上当前的结点值,那么对于例子1来说,根结点的结点值就变成了30,断开位置的结点就变成了15,那么我们就可以发现其实只要断开位置的结点值是根结点值的一半,就存在等价划分。所以这道题的难点就是更新每个结点的结点值,我们可以使用递归来做。博主最开始使用的是unordered_set,把更新后的每个结点值都存入集合中,但是对于test case: [0, 1, -1] 会fail, 仔细分析下这个case,发现更新后的根结点值还是0,而且0已经被存入集合了,而0除以2还是0,在集合中存在,会返回true,但其实这棵树是不能等价划分的。0的情况确实比较特殊,所以我们使用unordered_map,建立更新后的结点值和其出现次数之间的映射,这样只有map中0的个数大于1的时候,才返回true。这样完美的避开了根结点为0的陷阱
https://discuss.leetcode.com/topic/100179/java-c-simple-solution-with-only-one-hashmap
The idea is to use a hash table to record all the different sums of each subtree in the tree. If the total sum of the tree is
sum
, we just need to check if the hash table constains sum/2
.
The following code has the correct result at a special case when the tree is
[0,-1,1]
, which many solutions dismiss. I think this test case should be added. public boolean checkEqualTree(TreeNode root) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
int sum = getsum(root, map);
if(sum == 0)return map.getOrDefault(sum, 0) > 1;
return sum%2 == 0 && map.containsKey(sum/2);
}
public int getsum(TreeNode root, Map<Integer, Integer> map ){
if(root == null)return 0;
int cur = root.val + getsum(root.left, map) + getsum(root.right, map);
map.put(cur, map.getOrDefault(cur,0) + 1);
return cur;
}
http://blog.csdn.net/TheSnowBoy_2/article/details/77448762 public boolean checkEqualTree(TreeNode root) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
int sum = getsum(root, map);
if(sum == 0)return map.getOrDefault(sum, 0) > 1;
return sum%2 == 0 && map.containsKey(sum/2);
}
public int getsum(TreeNode root, Map<Integer, Integer> map ){
if(root == null)return 0;
int cur = root.val + getsum(root.left, map) + getsum(root.right, map);
map.put(cur, map.getOrDefault(cur,0) + 1);
return cur;
}
https://discuss.leetcode.com/topic/100126/java-solution-tree-traversal-and-sum boolean equal = false;
long total = 0;
public boolean checkEqualTree(TreeNode root) {
if (root.left == null && root.right == null) return false;
total = getTotal(root);
checkEqual(root);
return equal;
}
private long getTotal(TreeNode root) {
if (root == null) return 0;
return getTotal(root.left) + getTotal(root.right) + root.val;
}
private long checkEqual(TreeNode root) {
if (root == null || equal) return 0;
long curSum = checkEqual(root.left) + checkEqual(root.right) + root.val;
if (total - curSum == curSum) {
equal = true;
return 0;
}
return curSum;
}
http://weihan.online/blog/eugenejw.github.io/_site/2017/10/leetcode-663.html private int total = 0;
private boolean ret = false;
private TreeNode originRoot = null;
public boolean checkEqualTree(TreeNode root) {
originRoot = root;
total = getTotal(root);
checkEqual(root);
return ret;
}
private int getTotal(TreeNode root) {
// post-order DFS
if (root==null) return 0;
return getTotal(root.left) + getTotal(root.right) + root.val;
}
private int checkEqual(TreeNode root) {
// post-order DFS
if (root==null || ret) return 0; // skip checking
int curSum = checkEqual(root.left) + checkEqual(root.right) + root.val;
if (total-curSum==curSum) {
if (root!=originRoot) { // skip the top level root
ret = true;
return 0;
}
}
return curSum;
}
X. https://blog.csdn.net/magicbean2/article/details/79183244
bool checkEqualTree(TreeNode* root) {
unordered_map<TreeNode*, int> hash; // node -> sum
int sum = getSum(root, hash);
if (sum % 2 != 0) {
return false;
}
return canSplit(root, hash, sum / 2);
}
private:
bool canSplit(TreeNode* root, unordered_map<TreeNode*, int> &hash, int half_sum) {
if (root == NULL) {
return false;
}
if (root->left && hash[root->left] == half_sum) {
return true;
}
if (root->right && hash[root->right] == half_sum) {
return true;
}
return canSplit(root->left, hash, half_sum) || canSplit(root->right, hash, half_sum);
}
int getSum(TreeNode* root, unordered_map<TreeNode*, int> &hash) {
if (root == NULL) {
return 0;
}
int left = getSum(root->left, hash);
int right = getSum(root->right, hash);
int sum = left + right + root->val;
hash[root] = sum;
return sum;
}