java 663. Equal Tree Partition.java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 663. Equal Tree Partition.java相关的知识,希望对你有一定的参考价值。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    private int dfs(Set<Integer> subTreeSum, TreeNode root) {
        if (root == null) return 0;
        if (root.left == null && root.right == null) {
            subTreeSum.add(root.val);
            return root.val;
        }
        int left = dfs(subTreeSum, root.left);
        subTreeSum.add(left);
        int right = dfs(subTreeSum, root.right);
        subTreeSum.add(right);
        return left + right + root.val;
    }
    public boolean checkEqualTree(TreeNode root) {
        if (root == null || (root.left == null && root.right == null)) return false;
        Set<Integer> subTreeSum = new HashSet<>();
        int total = dfs(subTreeSum, root);
        if (total % 2 != 0) return false;
        return subTreeSum.contains(total/2);
    }
}

以上是关于java 663. Equal Tree Partition.java的主要内容,如果未能解决你的问题,请参考以下文章

663. Equal Tree Partition

[leetcode-663-Equal Tree Partition]

663. Equal Tree Partition 能否把树均分为求和相等的两半

leetcode_easy_array1013. Partition Array Into Three Parts With Equal Sum

equal_range 应该如何工作?

Day663.大佬学习经验分享 -Java业务开发常见错误