979. Distribute Coins in Binary Tree #204
-
Topics: You are given the 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:
Example 2:
Constraints:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The problem requires redistributing coins in a binary tree such that each node ends up with exactly one coin. You are given a binary tree with Key Points
ApproachThe key to solving this problem lies in using Depth First Search (DFS) to traverse the binary tree and balance the coins. We aim to track the number of excess coins at each node and ensure that excess coins are moved to nodes that have fewer than one coin. Here's how to approach the problem:
Plan
Let's implement this solution in PHP: 979. Distribute Coins in Binary Tree <?php
/**
* @param TreeNode $root
* @return Integer
*/
function distributeCoins(TreeNode $root): int
{
$totalMoves = 0; // This will store the total number of moves needed to balance the coins
// The Depth First Search (DFS) function computes the number of moves required
// starting from the leaves up to the root of the tree.
// It returns the excess number of coins that need to be moved from the current node.
$dfs = function($node) use (&$dfs, &$totalMoves) {
// Base case: if the current node is null, return 0 since there are no coins to move.
if (!$node) {
return 0;
}
// Recursive case: solve for left and right subtrees.
$leftMoves = $dfs($node->left);
$rightMoves = $dfs($node->right);
// The number of moves made at the current node is the sum of absolute values of each subtree’s excess coins.
// Because moves from both left and right need to pass through the current node.
$totalMoves += abs($leftMoves) + abs($rightMoves);
// Return the number of excess coins at this node: positive if there are more coins than nodes,
// negative if there are fewer. A value of -1 means just the right amount for the node itself.
return $leftMoves + $rightMoves + $node->val - 1;
};
// Call the DFS function starting from the root of the tree.
$dfs($root);
// Return the total number of moves needed to distribute the coins.
return $totalMoves;
}
// Example 1
$root = [3,0,0];
echo distributeCoins($root); // Output: 2
// Example 2
$root = [0,3,0];
echo distributeCoins($root); // Output: 3
?>
Explanation:
Example WalkthroughExample 1:
Example 2:
Time Complexity
Output for Example
This problem can be efficiently solved using Depth First Search (DFS). By calculating the excess coins for each node and accumulating the required moves, we can determine the minimum number of moves to redistribute the coins such that every node has exactly one. The approach is optimal with a time complexity of O(n), making it suitable for trees with up to 100 nodes. |
Beta Was this translation helpful? Give feedback.
The problem requires redistributing coins in a binary tree such that each node ends up with exactly one coin. You are given a binary tree with
n
nodes, where each node contains a certain number of coins. The goal is to determine the minimum number of moves needed to ensure every node has exactly one coin. A move consists of transferring a coin between adjacent nodes, either from a parent to a child or vice versa.Key Points
n
nodes.Approach
T…