Skip to content

Sum of Binary Tree Node Tilts

Sar Champagne Bielert edited this page May 17, 2024 · 4 revisions

Unit 9 Session 2 (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Medium
  • Time to complete: 30+ mins
  • 🛠️ Topics: Tree, Depth-First Search, Recursion

1: U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Established a set (2-3) of test cases to verify their own solution later.
  • Established a set (1-2) of edge cases to verify their solution handles complexities.
  • Have fully understood the problem and have no clarifying questions.
  • Have you verified any Time/Space Constraints for this problem?
  • Can the tree be empty?

    • Yes, if the tree is empty, return 0.
  • What should be returned if the tree has only one node?

    • The tilt of a single node tree is 0.
HAPPY CASE

    1 (root)
   / \
  2   3

Input: root
Output: 1
Explanation: 
- Node 1: left sum = 2, right sum = 3, tilt = |2 - 3| = 1
- Node 2: left sum = 0, right sum = 0, tilt = |0 - 0| = 0
- Node 3: left sum = 0, right sum = 0, tilt = |0 - 0| = 0
- Total tilt = 1 + 0 + 0 = 1
EDGE CASE

    1 (root)

Input: root
Output: 0
Explanation: The tree has only one node, so the tilt is 0.

2: M-atch

Match what this problem looks like to known categories of problems, e.g., Linked List or Dynamic Programming, and strategies or patterns in those categories.

For Tree problems, we want to consider the following approaches:

  • Depth-First Search (DFS): Useful for calculating the sum of subtrees and tilts.
  • Recursion: Effective for traversing and updating the tree.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Use a DFS approach to traverse the tree. For each node, calculate the sum of the left and right subtrees and their tilt. Keep a running total of the tilts and return it at the end.

1) Define a helper function `dfs(node, total_tilt)` to calculate the tilt and subtree sums.
    a) If the current node is None, return 0.
    b) Recursively calculate the sum of the left subtree.
    c) Recursively calculate the sum of the right subtree.
    d) Calculate the tilt of the current node as the absolute difference between left and right subtree sums.
    e) Add the current node's tilt to the total tilt.
    f) Return the sum of values of the subtree rooted at the current node.
2) In the main function `find_tilt(root)`, initialize total_tilt to 0.
3) Call the helper function to start the DFS from the root.
4) Return the total tilt.

⚠️ Common Mistakes

  • Forgetting to handle the case where the tree is empty.
  • Not correctly updating the total tilt for each node.

4: I-mplement

Implement the code to solve the algorithm.

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def dfs(node, total_tilt):
    if not node:
        return 0
    
    # Recursive call to calculate the sum of left and right subtrees
    left_sum = dfs(node.left, total_tilt)
    right_sum = dfs(node.right, total_tilt)
    
    # Tilt of the current node
    tilt = abs(left_sum - right_sum)
    
    # Add the current node's tilt to the total tilt
    total_tilt[0] += tilt
    
    # Return the sum of values of the subtree rooted at the current node
    return node.val + left_sum + right_sum
    
def find_tilt(root):
    total_tilt = [0]  # Use a list to allow modification within dfs function
    dfs(root, total_tilt)
    return total_tilt[0]

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

  • Trace through your code with an input to check for the expected output
  • Catch possible edge cases and off-by-one errors
  • For a starting point, try checking your code against the Happy/Edge Case(s) above

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

Time Complexity:

  • O(N), where N is the number of nodes in the tree.
  • This is because we must visit each node exactly once.

Space Complexity:

  • O(N), where N is the number of nodes in the tree.
  • This is because the recursion stack could hold up to N nodes in the worst case (when the tree is completely unbalanced)."
Clone this wiki locally