979. Distribute Coins in Binary Tree LeetCode Solution

In this guide, you will get 979. Distribute Coins in Binary Tree LeetCode Solution with the best time and space complexity. The solution to Distribute Coins in Binary Tree problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Distribute Coins in Binary Tree solution in C++
  4. Distribute Coins in Binary Tree solution in Java
  5. Distribute Coins in Binary Tree solution in Python
  6. Additional Resources
979. Distribute Coins in Binary Tree LeetCode Solution image

Problem Statement of Distribute Coins in Binary Tree

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.

See also  332. Reconstruct Itinerary LeetCode Solution

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.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(h)

979. Distribute Coins in Binary Tree LeetCode Solution in C++

class Solution {
 public:
  int distributeCoins(TreeNode* root) {
    int ans = 0;
    dfs(root, ans);
    return ans;
  }

 private:
  // Returns the number of coins I can give (positive) / take (negative).
  int dfs(TreeNode* root, int& ans) {
    if (root == nullptr)
      return 0;

    const int l = dfs(root->left, ans);
    const int r = dfs(root->right, ans);
    ans += abs(l) + abs(r);
    return (root->val - 1) + l + r;
  }
};
/* code provided by PROGIEZ */

979. Distribute Coins in Binary Tree LeetCode Solution in Java

class Solution {
  public int distributeCoins(TreeNode root) {
    dfs(root);
    return ans;
  }

  private int ans = 0;

  // Returns the number of coins I can give (positive) / take (negative).
  private int dfs(TreeNode root) {
    if (root == null)
      return 0;

    final int l = dfs(root.left);
    final int r = dfs(root.right);
    ans += Math.abs(l) + Math.abs(r);

    return (root.val - 1) + l + r;
  }
}
// code provided by PROGIEZ

979. Distribute Coins in Binary Tree LeetCode Solution in Python

N/A
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.