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 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
3001 coin1 coinCoin Distribution FlowRed nodes: Have excess coins (more than 1)Green nodes: Need coins (less than 1)Arrows: Show direction and amount of coin flowTotal moves: Sum of absolute flows = 1 + 1 = 2
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

n
2n
โœ“ Linear Growth
Space Complexity
O(h)

Recursion stack depth equals tree height h, O(log n) for balanced tree, O(n) worst case

n
2n
โœ“ Linear Space

Constraints

  • The number of nodes in the tree is n
  • 1 โ‰ค n โ‰ค 100
  • 0 โ‰ค Node.val โ‰ค n
  • The sum of all Node.val is n
  • Each node initially has between 0 and n coins
Asked in
Google 25 Amazon 18 Meta 12 Microsoft 8
89.5K Views
Medium Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen