Advertisement

Distribute Coins in Binary Tree - LeetCode 979 Solution

Distribute Coins in Binary Tree - Complete Solution Guide

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

Problem Statement

You are given the root of a binary tree with n nodes where each node in the tree has node.val coins. There are n coins in total throughout the whole tree. In one move, we may choose two adjacent nodes and move one coin from one node to another. A move may be from parent to child, or from child to parent. Return the minimum number of moves required to make every node have exactly one coin . Example 1: Input: root = [3,0,0] Output: 2 Explanation: From the root of the tree, we move one coin to its

Detailed Explanation

The problem asks us to find the minimum number of moves required to distribute coins in a binary tree such that each node has exactly one coin. A move consists of transferring a coin between adjacent nodes (parent to child or child to parent). The total number of coins in the tree is equal to the number of nodes.

Solution Approach

The solution uses a Depth-First Search (DFS) algorithm to traverse the binary tree. For each node, we calculate its balance by recursively calculating the balance of its left and right children. The absolute values of the left and right children balances are added to a global `moves` variable. Then, the balance of the current node is calculated as the sum of its own value, the left balance, the right balance, minus 1 (since each node should ideally have one coin). This balance is returned to the parent node to be used in the parent's balance calculation.

Step-by-Step Algorithm

  1. Step 1: Initialize a global variable `moves` to 0. This variable will store the total number of moves required.
  2. Step 2: Define a recursive DFS function that takes a node as input.
  3. Step 3: In the DFS function, if the node is null, return 0 (no balance).
  4. Step 4: Recursively call the DFS function on the left child and store the returned balance in `left_balance`.
  5. Step 5: Recursively call the DFS function on the right child and store the returned balance in `right_balance`.
  6. Step 6: Add the absolute values of `left_balance` and `right_balance` to the `moves` variable.
  7. Step 7: Calculate the current node's balance as `node.val + left_balance + right_balance - 1`. This represents the number of coins this node is either over or under.
  8. Step 8: Return the current node's balance to the parent node.
  9. Step 9: Call the DFS function on the root node.
  10. Step 10: Return the `moves` variable, which now contains the total number of moves required.

Key Insights

  • Insight 1: The core idea is to realize that the number of moves is determined by the 'balance' of each node. The balance is defined as the difference between the number of coins a node has and the number of coins it *should* have (which is 1).
  • Insight 2: We can use Depth-First Search (DFS) to traverse the tree and calculate the balance for each node in a bottom-up manner. The absolute value of the balance represents the number of moves needed involving that node.
  • Insight 3: The balance can be positive (excess coins) or negative (deficit of coins). The excess/deficit can be pushed to/pulled from the parent node.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(h)

Topics

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

Companies

Asked at: PhonePe.