<p>You are given the <code>root</code> of a binary tree with <code>n</code> nodes where each <code>node</code> in the tree has <code>node.val</code> coins. There are <code>n</code> coins in total throughout the whole tree.</p>

<p>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.</p>

<p>Return <em>the <strong>minimum</strong> number of moves required to make every node have <strong>exactly</strong> one coin</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/01/18/tree1.png" style="width: 250px; height: 236px;" />
<pre>
<strong>Input:</strong> root = [3,0,0]
<strong>Output:</strong> 2
<strong>Explanation: </strong>From the root of the tree, we move one coin to its left child, and one coin to its right child.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/01/18/tree2.png" style="width: 250px; height: 236px;" />
<pre>
<strong>Input:</strong> root = [0,3,0]
<strong>Output:</strong> 3
<strong>Explanation: </strong>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.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is <code>n</code>.</li>
	<li><code>1 &lt;= n &lt;= 100</code></li>
	<li><code>0 &lt;= Node.val &lt;= n</code></li>
	<li>The sum of all <code>Node.val</code> is <code>n</code>.</li>
</ul>

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def distributeCoins(self, root: Optional[TreeNode]) -> int:
        '''
        n total nodes and n total coins, so an answer is always possible
        coins must move along paths, from child to parent or parent to child

        if a leaf node has 0 coins, it takes one operation  (parent to leaf)
        if a leaf node has n > 1 coins, it takes n - 1 operations to balance move to parent

        think of operations as only push
        a node will push to children if that nodes total coinage is too high
        a subtree will push to parents if that subtrees total coinage is too high

        let dfs(node) 
            return positive if coming in > going out
            return negative if going out > coming in
        if a leaf node has 0 coins,
            dfs(leaf) = 1
        if leaf node has 3 coins
            dfs(leaf) = -2
        
        for example 1
            dfs(left child) = 1
            dfs(right child) = 1
            parent node has 3 - 1 - 1 = 1 coins, no more operations needed
        for example 2
            dfs(left child) = -2
            dfs(right child) = 1
            parent node has 0 - (-2) - 1
                = 2 - 1 = 1 coins, no more operations needed

        minimum number of operations is abs(dfs(left child)) + abs(dfs(right child))
        coins held by parent is parent.val - dfs(left) - dfs(right)
        return coins held by parent

        time: O(N)
        '''
        return self.solveDfs(root)
    
    def solveDfs(self, root):
        operations = 0

        def dfs(node):
            nonlocal operations

            left = dfs(node.left) if node.left else 0
            right = dfs(node.right) if node.right else 0

            operations += abs(left) + abs(right)

            return 1 - (node.val - left - right)

        dfs(root)
        return operations
16600