-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0979-distribute-coins-in-binary-tree.js
43 lines (38 loc) · 1.22 KB
/
0979-distribute-coins-in-binary-tree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* 979. Distribute Coins in Binary Tree
* https://leetcode.com/problems/distribute-coins-in-binary-tree/
* Difficulty: Medium
*
* 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.
*/
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var distributeCoins = function(root) {
let result = 0;
traverse(root);
return result;
function traverse(node) {
if (!node) return 0;
const leftExcess = traverse(node.left);
const rightExcess = traverse(node.right);
const totalExcess = node.val + leftExcess + rightExcess - 1;
result += Math.abs(leftExcess) + Math.abs(rightExcess);
return totalExcess;
}
};