541. Equal Tree Partition
Given the root of a binary tree, return true if you can partition the tree into two trees with equal sums of values after removing exactly one edge on the original tree.
Example 1:
Input: root = [5,10,10,null,null,2,3]
Output: true
Explanation: The total sum of the tree is 5 + 10 + 10 + 2 + 3 = 30. If we remove the edge between the root (5) and one of its children (10), we can create two trees with sums 10 and 20, which is not equal. However, removing the edge between 10 (right child) and its child 2 creates two subtrees with sums 15 and 15, making partition possible.
Example 2:
Input: root = [1,2,10,null,null,2,20]
Output: false
Explanation: The total sum of the tree is 1 + 2 + 10 + 2 + 20 = 35, which is an odd number. Since an odd number cannot be evenly split into two equal integer sums, partitioning the tree into two equal-sum subtrees is impossible.
Now Your Turn!
Pick the correct output for the given inputInput: root = [23,1,null,null,2,24,null,3,null,1,null,null,null]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- The number of nodes in the tree is in the range [1, 104].
- -105 <= Node.val <= 105