Find the minimum moves to give every binary tree node exactly one coin

Quick Overview

Find the fewest single-coin moves along parent-child edges that leave every node of a binary tree holding exactly one coin. It tests bottom-up tree traversal and a counting argument that shows the result is the true minimum.

Find the minimum moves to give every binary tree node exactly one coin

Company: Zepto

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given the `root` of a binary tree with `n` nodes, where each node holds `node.val` coins. There are exactly `n` coins in total across the whole tree. In one move, you may choose two adjacent nodes and move one coin from one to the other. A move may go from parent to child or from child to parent. Return the minimum number of moves required to make every node have exactly one coin. ### Function Signature ```python def distribute_coins(root: list[int | None]) -> int: ``` ### Rules - The tree is given as a level-order list. The first entry is the root. After that, each present node, in breadth-first order, owns two slots: its left child, then its right child. `None` marks a missing child, and a missing node owns no slots. Trailing `None` entries may be left out. - Each list value is the number of coins on that node. ### Constraints - `1 <= n <= 100`, where `n` is the number of nodes. - `0 <= node.val <= n` - The values of all nodes sum to `n`. ### Examples ```text Input: root = [0, 3, 0] Output: 3 0 / \ 3 0 ``` The left child has a surplus of 2 coins. It sends 2 coins up to the root (2 moves), and the root passes 1 of them down to the right child (1 move), for 3 moves in total. ```text Input: root = [0, 0, None, 4, None, 0] Output: 4 0 / 0 / 4 / 0 ``` The node with 4 coins keeps one, sends one to its child (1 move) and sends two to its parent (2 moves). The parent keeps one and sends the other to the root (1 move), so the total is 1 + 2 + 1 = 4. ```text Input: root = [1, 0, 0, None, 3] Output: 4 1 / \ 0 0 \ 3 ``` The node with 3 coins sends 2 up to its parent (2 moves). That parent keeps one and passes one to the root (1 move), and the root, now holding 2, sends one to its right child (1 move).

Overview: Find the fewest single-coin moves along parent-child edges that leave every node of a binary tree holding exactly one coin. It tests bottom-up tree traversal and a counting argument that shows the result is the true minimum.

Zepto logo
Zepto
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given the root of a binary tree with n nodes, where each node holds node.val coins. There are exactly n coins in total across the whole tree.

In one move, you may choose two adjacent nodes and move one coin from one to the other. A move may go from parent to child or from child to parent.

Return the minimum number of moves required to make every node have exactly one coin.

Function Signature

def distribute_coins(root: list[int | None]) -> int:

Rules

  • The tree is given as a level-order list. The first entry is the root. After that, each present node, in breadth-first order, owns two slots: its left child, then its right child. None marks a missing child, and a missing node owns no slots. Trailing None entries may be left out.
  • Each list value is the number of coins on that node.

Constraints

  • 1 <= n <= 100 , where n is the number of nodes.
  • 0 <= node.val <= n
  • The values of all nodes sum to n .

Examples

Input: root = [0, 3, 0]
Output: 3

    0
   / \
  3   0

The left child has a surplus of 2 coins. It sends 2 coins up to the root (2 moves), and the root passes 1 of them down to the right child (1 move), for 3 moves in total.

Input: root = [0, 0, None, 4, None, 0]
Output: 4

        0
       /
      0
     /
    4
   /
  0

The node with 4 coins keeps one, sends one to its child (1 move) and sends two to its parent (2 moves). The parent keeps one and sends the other to the root (1 move), so the total is 1 + 2 + 1 = 4.

Input: root = [1, 0, 0, None, 3]
Output: 4

      1
     / \
    0   0
     \
      3

The node with 3 coins sends 2 up to its parent (2 moves). That parent keeps one and passes one to the root (1 move), and the root, now holding 2, sends one to its right child (1 move).