Given the root
of a binary tree, return the sum of every tree node's tilt.
The tilt of a tree node is the absolute difference between the sum of all left subtree node values and all right subtree node values. If a node does not have a left child, then the sum of the left subtree node values is treated as 0
. The rule is similar if the node does not have a right child.
Example 1:
Input: root = [1,2,3] Output: 1 Explanation: Tilt of node 2 : |0-0| = 0 (no children) Tilt of node 3 : |0-0| = 0 (no children) Tilt of node 1 : |2-3| = 1 (left subtree is just left child, so sum is 2; right subtree is just right child, so sum is 3) Sum of every tilt : 0 + 0 + 1 = 1
Example 2:
Input: root = [4,2,9,3,5,null,7] Output: 15 Explanation: Tilt of node 3 : |0-0| = 0 (no children) Tilt of node 5 : |0-0| = 0 (no children) Tilt of node 7 : |0-0| = 0 (no children) Tilt of node 2 : |3-5| = 2 (left subtree is just left child, so sum is 3; right subtree is just right child, so sum is 5) Tilt of node 9 : |0-7| = 7 (no left child, so sum is 0; right subtree is just right child, so sum is 7) Tilt of node 4 : |(3+5+2)-(9+7)| = |10-16| = 6 (left subtree values are 3, 5, and 2, which sums to 10; right subtree values are 9 and 7, which sums to 16) Sum of every tilt : 0 + 0 + 0 + 2 + 7 + 6 = 15
Example 3:
Input: root = [21,7,14,1,1,2,2,3,3] Output: 9
Constraints:
[0, 104]
.-1000 <= Node.val <= 1000
When 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 find the tilt of a binary tree involves examining every single node in the tree. For each node, we'll calculate the tilt by computing the sum of the node's left subtree and the sum of the node's right subtree, then finding the absolute difference. Finally, we add up all these differences.
Here's how the algorithm would work step-by-step:
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def find_tree_tilt(root):
total_tilt = 0
def calculate_subtree_sum(node):
if not node:
return 0
return node.val + calculate_subtree_sum(node.left) + calculate_subtree_sum(node.right)
def traverse_tree(node):
nonlocal total_tilt
if not node:
return
#Calculate the sum of the left subtree
left_subtree_sum = calculate_subtree_sum(node.left)
#Calculate the sum of the right subtree
right_subtree_sum = calculate_subtree_sum(node.right)
# The tilt is the absolute difference of the subtree sums.
tilt = abs(left_subtree_sum - right_subtree_sum)
# Add to the running sum.
total_tilt += tilt
traverse_tree(node.left)
traverse_tree(node.right)
traverse_tree(root)
return total_tilt
The most efficient way to find the total tilt of a binary tree involves calculating the tilt of each node and accumulating the results in a single pass. We achieve this by using a method that figures out the sum of values in the subtrees while simultaneously calculating each node's tilt. This prevents revisiting nodes and keeps the process efficient.
Here's how the algorithm would work step-by-step:
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def findTilt(self, root: TreeNode) -> int:
total_tilt_sum = 0
def calculate_node_sum_and_tilt(node: TreeNode) -> int:
nonlocal total_tilt_sum
if not node:
return 0
# Calculate sum of left and right subtrees
left_subtree_sum = calculate_node_sum_and_tilt(node.left)
right_subtree_sum = calculate_node_sum_and_tilt(node.right)
# Tilt is absolute difference between left and right
current_node_tilt = abs(left_subtree_sum - right_subtree_sum)
total_tilt_sum += current_node_tilt
# Return sum of current node and its subtrees
return node.val + left_subtree_sum + right_subtree_sum
calculate_node_sum_and_tilt(root)
return total_tilt_sum
Case | How to Handle |
---|---|
Null root node | Return 0 as the tilt of a null tree is defined as 0. |
Single node tree | Return 0 as a single node tree has no tilt. |
Tree with all nodes having value 0 | This should be handled correctly as the sum of subtrees would be 0, and tilt would be calculated accurately. |
Tree with large positive and negative values causing potential integer overflow in sum | Use a larger integer type (e.g., long) to store subtree sums to avoid potential overflow. |
Complete binary tree with maximum depth and node values | Ensure that the recursion depth doesn't exceed the stack limit and algorithm is efficient enough for large trees by considering iterative solutions. |
Skewed tree (left or right) | Verify the solution handles the unbalanced nature of the tree efficiently, avoiding worst-case O(n^2) time complexity in some implementations. |
Tree with negative node values | The algorithm should correctly handle negative values during the summation of subtrees and tilt calculation. |
Tree with extreme value differences between left and right subtrees | Confirm that the absolute difference calculation doesn't cause unexpected behavior with very large numbers; use appropriate data types. |