Distribute Coins in Binary Tree - Problem
Distribute Coins in Binary Tree
You are given the root of a binary tree with n nodes where each node contains
In one move, you can transfer one coin between two adjacent nodes (parent-child or child-parent). Your goal is to redistribute the coins so that every node has exactly one coin.
Return the minimum number of moves required to achieve this balanced distribution.
Example: If a node has 3 coins and needs only 1, it must give away 2 coins. If a node has 0 coins, it needs to receive 1 coin from a neighbor.
Key insight: This is a classic tree rebalancing problem where we need to calculate the flow of excess/deficit coins through each edge.
You are given the root of a binary tree with n nodes where each node contains
node.val coins. The total number of coins in the entire tree equals exactly n (the number of nodes).In one move, you can transfer one coin between two adjacent nodes (parent-child or child-parent). Your goal is to redistribute the coins so that every node has exactly one coin.
Return the minimum number of moves required to achieve this balanced distribution.
Example: If a node has 3 coins and needs only 1, it must give away 2 coins. If a node has 0 coins, it needs to receive 1 coin from a neighbor.
Key insight: This is a classic tree rebalancing problem where we need to calculate the flow of excess/deficit coins through each edge.
Input & Output
example_1.py โ Basic Tree
$
Input:
[3, 0, 0]
โบ
Output:
2
๐ก Note:
The root has 3 coins but needs only 1. Both children have 0 coins but need 1 each. So we move 1 coin from root to left child and 1 coin from root to right child. Total moves = 2.
example_2.py โ Unbalanced Tree
$
Input:
[0, 3, 0]
โบ
Output:
3
๐ก Note:
Left child has 3 coins but needs only 1, so it has 2 excess. Root and right child each need 1 coin. We move 1 coin from left child to root (1 move), then 1 coin from root to right child (1 move), and 1 more coin from left child through root to right child (1 more move through root). Total = 3 moves.
example_3.py โ Single Node
$
Input:
[1]
โบ
Output:
0
๐ก Note:
The tree has only one node with exactly 1 coin, which is already the target state. No moves are needed.
Visualization
Tap to expand
Understanding the Visualization
1
Identify Imbalances
Mark nodes with excess coins (red) and nodes needing coins (green)
2
Calculate Subtree Needs
Each subtree calculates total excess or deficit
3
Flow Through Edges
Coins flow from excess areas to deficit areas through tree edges
4
Count Total Flow
Sum absolute values of flow through all edges
Key Takeaway
๐ฏ Key Insight: Every coin that moves passes through exactly one edge in the tree. By calculating the net flow through each edge using post-order DFS, we can count the minimum moves in a single traversal.
Time & Space Complexity
Time Complexity
O(n)
Single pass through all n nodes in the tree
โ Linear Growth
Space Complexity
O(h)
Recursion stack depth equals tree height h, O(log n) for balanced tree, O(n) worst case
โ Linear Space
Constraints
-
The number of nodes in the tree is
n -
1 โค n โค 100 -
0 โค Node.val โค n -
The sum of all
Node.valisn - Each node initially has between 0 and n coins
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code