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
Example 2:
Input: root = [1,2,10,null,null,2,20] Output: false Explanation: You cannot split the tree into two trees with equal sums after removing exactly one edge on the tree.
Constraints:
[1, 104].-105 <= Node.val <= 105Problem Overview: You are given a binary tree and can remove exactly one edge. The goal is to determine whether that cut can split the tree into two subtrees whose node values sum to the same total.
The core observation: if the total sum of all nodes is S, you need a subtree whose sum equals S/2. Removing the edge above that subtree creates two trees with equal sums.
Approach 1: Recompute Subtree Sum for Every Edge (Brute Force) (Time: O(n²), Space: O(h))
Consider every edge in the tree as a potential cut. For each edge, compute the sum of nodes in the detached subtree using a DFS traversal, then compare it with the remaining part of the tree. If both sums match, the partition works. This approach repeatedly recalculates subtree sums, causing redundant traversals of the same nodes. It works for small trees but becomes slow for larger inputs since each DFS can take O(n) time and you may try up to O(n) edges.
Approach 2: Single DFS with Subtree Sum Tracking (Optimal) (Time: O(n), Space: O(n))
Run a postorder DFS over the binary tree and compute the sum of every subtree. Store each subtree sum in a hash set or frequency map while returning sums up the recursion stack. After the traversal, you know the total tree sum. If the total is odd, equal partition is impossible. Otherwise check whether a subtree with sum total/2 exists. Cutting the edge above that subtree forms two trees with equal sums.
Handling the edge case where the total sum is zero requires special care. Multiple zero-sum subtrees must exist because removing the edge above the entire tree is not allowed. Tracking subtree frequencies solves this.
This solution performs a single pass using depth-first search and leverages subtree aggregation, a common pattern in tree problems. Postorder traversal ensures children are processed before computing the parent’s sum.
Recommended for interviews: The single DFS subtree-sum approach. Interviewers expect you to recognize that equal partition requires a subtree with half of the total sum. The brute force method demonstrates understanding of the problem structure, but the O(n) DFS solution shows you know how to aggregate information efficiently during traversal.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recompute Subtree Sum for Each Edge (Brute Force) | O(n²) | O(h) | Conceptual baseline or very small trees where repeated DFS is acceptable |
| Single DFS with Subtree Sum Hashing | O(n) | O(n) | General case and interview solution; computes all subtree sums in one traversal |
Tree Data Structure | Lecture 23 - Equal Tree Partition • The Hustling Engineer • 1,480 views views
Watch 5 more video solutions →Practice Equal Tree Partition with our built-in code editor and test cases.
Practice on FleetCode