Solving Leetcode Interviews in Seconds with AI: Binary Tree Tilt
Introduction
In this blog post, we will explore how to solve the LeetCode problem "563" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
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: The number of nodes in the tree is in the range [0, 104]. -1000 <= Node.val <= 1000
Explanation
Here's a breakdown of the solution:
- Post-order Traversal: Use a post-order traversal of the binary tree to calculate the sum of subtree nodes. This ensures that the sums of the left and right subtrees are computed before calculating the tilt of the current node.
- Tilt Calculation: For each node, calculate the tilt as the absolute difference between the sum of its left subtree and the sum of its right subtree. Accumulate the tilts.
Return Sum: Return the accumulated sum of all tilts.
Time & Space Complexity: The solution has O(N) time complexity, where N is the number of nodes in the tree, because each node is visited once. It has O(H) space complexity, where H is the height of the tree, due to the recursive call stack. In the worst case (skewed tree), H can be N, leading to O(N) space complexity. In the average case (balanced tree), H is log(N), leading to O(log(N)) space complexity.
Code
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def findTilt(root):
"""
Calculates the sum of tilts of all nodes in a binary tree.
Args:
root: The root node of the binary tree.
Returns:
The sum of the tilts of all nodes in the binary tree.
"""
tilt_sum = 0
def subtree_sum_and_tilt(node):
"""
Calculates the sum of the subtree rooted at the given node and updates the global tilt_sum.
Args:
node: The root of the subtree.
Returns:
The sum of the values of all nodes in the subtree.
"""
nonlocal tilt_sum
if not node:
return 0
left_sum = subtree_sum_and_tilt(node.left)
right_sum = subtree_sum_and_tilt(node.right)
tilt = abs(left_sum - right_sum)
tilt_sum += tilt
return node.val + left_sum + right_sum
subtree_sum_and_tilt(root)
return tilt_sum