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 left child, and one coin to its right child.
Example 2:
Input: root = [0,3,0] Output: 3 Explanation: From the left child of the root, we move two coins to the root [taking two moves]. Then, we move one coin from the root of the tree to the right child.
Constraints:
n.1 <= n <= 1000 <= Node.val <= nNode.val is n.The key idea in #979 Distribute Coins in Binary Tree is to balance coins across all nodes so that each node ends up with exactly one coin. Since coins can only move between adjacent nodes (parent and child), the problem naturally fits a Depth-First Search (DFS) traversal.
During a postorder DFS, process the left and right subtrees before the current node. Each subtree returns the number of excess or missing coins it contributes to its parent. A positive value means extra coins can be passed upward, while a negative value means coins are needed from the parent.
At each node, the number of moves required is determined by the absolute value of the balances coming from its children. Accumulate these moves globally while propagating the net balance upward.
This strategy works efficiently because every node is visited exactly once. The algorithm runs in O(n) time with O(h) space complexity due to the recursion stack, where n is the number of nodes and h is the tree height.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| DFS Postorder Traversal | O(n) | O(h) |
Nick White
To solve the problem, we perform a post-order traversal of the tree. During this traversal, for each node, we calculate the excess coins (i.e., the number of coins - 1) that need to be moved either up to the parent or down to the children. We accumulate the absolute values of excess coins for each move.
Time Complexity: O(n), where n is the number of nodes in the tree. We visit each node exactly once.
Space Complexity: O(h), where h is the height of the tree due to the recursion stack.
1public class TreeNode {
2 public int val;
3 public TreeNode left;
4 public TreeNode right;
5 public TreeNode(int x) { val = x; }
6}
7
8public class Solution {
9 private int moves = 0;
10
11 public int DistributeCoins(TreeNode root) {
12 Dfs(root);
13 return moves;
14 }
15
16 private int Dfs(TreeNode node) {
17 if (node == null) return 0;
18 int leftExcess = Dfs(node.left);
19 int rightExcess = Dfs(node.right);
20 moves += Math.Abs(leftExcess) + Math.Abs(rightExcess);
21 return node.val + leftExcess + rightExcess - 1;
22 }
23}This C# code follows the similar DFS approach to calculate and accumulate moves necessary for balancing coins. It uses attributes to store and return the number of moves globally.
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, variations of this problem appear in technical interviews at major tech companies. It tests understanding of tree traversal, DFS recursion, and how to propagate information from child nodes to parent nodes.
A binary tree structure combined with recursion is the most suitable. DFS traversal allows you to process child nodes first and propagate coin balances upward efficiently.
The optimal approach uses a postorder Depth-First Search (DFS). Each subtree calculates how many extra or missing coins it has and passes this balance to its parent while counting the required moves.
Postorder traversal ensures that both left and right subtrees are processed before evaluating the current node. This allows the algorithm to correctly compute coin balances from children and determine the number of moves needed.