Advertisement

Binary Tree Tilt - LeetCode 563 Solution

Binary Tree Tilt - Complete Solution Guide

Binary Tree Tilt is LeetCode problem 563, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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

Detailed Explanation

The problem asks us to calculate the sum of the 'tilt' of each node in a given binary tree. The 'tilt' of a node is defined as the absolute difference between the sum of all node values in its left subtree and the sum of all node values in its right subtree. If a node doesn't have a left or right child, we treat the respective subtree sum as 0. The input is the root of the binary tree, and the output is the total tilt of the entire tree.

Solution Approach

The solution uses a recursive Depth-First Search (DFS) algorithm to traverse the binary tree. The DFS function computes the sum of the subtree rooted at each node and simultaneously calculates the tilt of that node. A global variable (or a class member in object-oriented languages) accumulates the total tilt of the entire tree. The DFS function returns the sum of the subtree rooted at the current node to be used by the parent node in its tilt calculation.

Step-by-Step Algorithm

  1. Step 1: Initialize a variable `total_tilt` (or `tilt` in Java/C++) to 0 to store the sum of tilts.
  2. Step 2: Define a recursive function `dfs(node)` that takes a node as input.
  3. Step 3: Base case for the recursion: If the `node` is `None` (or `null`), return 0.
  4. Step 4: Recursively call `dfs` on the left child: `left_sum = dfs(node.left)`.
  5. Step 5: Recursively call `dfs` on the right child: `right_sum = dfs(node.right)`.
  6. Step 6: Calculate the tilt of the current `node`: `tilt = abs(left_sum - right_sum)`.
  7. Step 7: Add the calculated `tilt` to the `total_tilt` variable: `total_tilt += tilt`.
  8. Step 8: Return the sum of the subtree rooted at the current `node`: `return node.val + left_sum + right_sum`.
  9. Step 9: Call the `dfs` function with the `root` node.
  10. Step 10: Return the `total_tilt`.

Key Insights

  • Insight 1: The tilt of a node depends on the sum of the values of its left and right subtrees. Therefore, a post-order traversal is suitable because we need to calculate the subtree sums before we can determine the tilt of a parent node.
  • Insight 2: We can use Depth-First Search (DFS) to traverse the tree and compute the subtree sums and tilts recursively.
  • Insight 3: The total tilt can be calculated incrementally during the DFS traversal.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(h)

Topics

This problem involves: Tree, Depth-First Search, Binary Tree.

Companies

Asked at: Indeed.