Given the root of a binary tree, check whether there is at least one root to leaf path for which the sum of all the node values along the path equals the given targetSum. Besides, each node only could appear once in the path.
Return true if there exists a path whose sum equals the targetSum, and false otherwise.
Example 1:
Input: root = [5,10,null,3,null,1,11,null,null,null,2], targetSum = 22 Output: true Explanation: There exist a path whose sum equals the given targetSum.
Example 2:
Input: root = [1,2,3,4,5,6,7,8,9,10], targetSum = 15 Output: false Explanation: There exist no path whose sum equals the given targetSum.
Constraints:
[1, 104].-105 <= Node.val <= 105-108 <= targetSum <= 108When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to the Equal Tree Partition problem involves checking every possible way to split the tree into two parts by removing a single edge. We try each edge one by one, calculating the sum of the nodes on each side of the removed edge and checking if the sums are equal.
Here's how the algorithm would work step-by-step:
class TreeNode:
def __init__(self, value=0, left=None, right=None):
self.value = value
self.left = left
self.right = right
def equal_tree_partition_brute_force(root):
node_values = []
def traverse_tree(node):
if not node:
return 0
left_sum = traverse_tree(node.left)
right_sum = traverse_tree(node.right)
node_values.append(node.value + left_sum + right_sum)
return node.value + left_sum + right_sum
total_sum = traverse_tree(root)
# Check if the total sum is even, as odd sums cannot be partitioned equally
if total_sum % 2 != 0:
return False
target_sum = total_sum / 2
# Check for the existence of a subtree with sum equal to half of the total sum
for subtree_sum in node_values[:-1]:
if subtree_sum == target_sum:
return True
return FalseThe goal is to figure out if a tree can be split into two parts with equal sums. We calculate the total sum of all values in the tree and then check if there is any subtree that has a sum equal to half of the total sum.
Here's how the algorithm would work step-by-step:
class TreeNode:
def __init__(self, value=0, left=None, right=None):
self.value = value
self.left = left
self.right = right
class Solution:
def canPartition(self, root: TreeNode) -> bool:
total_tree_sum = self.calculate_tree_sum(root)
# If the total sum is odd, we can't partition.
if total_tree_sum % 2 != 0:
return False
target_subtree_sum = total_tree_sum // 2
subtree_sums = set()
def calculate_subtree_sum(node: TreeNode) -> int:
if not node:
return 0
left_subtree_sum = calculate_subtree_sum(node.left)
right_subtree_sum = calculate_subtree_sum(node.right)
current_subtree_sum = node.value + left_subtree_sum + right_subtree_sum
subtree_sums.add(current_subtree_sum)
return current_subtree_sum
calculate_subtree_sum(root)
# Check if any subtree sum equals half the total sum.
if target_subtree_sum in subtree_sums:
return True
return False
def calculate_tree_sum(self, root: TreeNode) -> int:
if not root:
return 0
return root.value + self.calculate_tree_sum(root.left) + self.calculate_tree_sum(root.right)| Case | How to Handle |
|---|---|
| Null root node (empty tree) | Return false immediately as an empty tree cannot be partitioned. |
| Single node tree | Return false because a single node cannot be partitioned into two subtrees. |
| Tree with all zero values | Handle this case by checking for a total sum of zero and if any non-root node also has a subtree sum of zero; return true if there is a subtree summing to zero. |
| Tree with only negative values | The algorithm should correctly calculate the sum of negative values and check if any subtree sums to half of the total sum. |
| Integer overflow for large node values | Use a larger data type (e.g., long) to store the sum of the subtree values to prevent integer overflow. |
| Total sum is odd | Return false immediately because if the total sum is odd, it cannot be divided into two equal integer subtrees. |
| Deeply skewed tree (e.g., linked list) | Recursive solution may exceed maximum call stack size; consider iterative solution or tail-call optimization (if language supports it). |
| Tree with a very large number of nodes | Ensure the chosen data structure (e.g., hash map for subtree sums) can handle the scale and optimize for memory usage. |