Distribute Coins in Binary Tree LeetCode solution in JavaScript

1
var distributeCoins = function (root) {
2
var moves = 0;
3
postorder(root);
4
return moves;
5

6
function postorder(node) {
7
if (!node) return 0;
8

9
const subTotal = postorder(node.left) + postorder(node.right);
10
const result = node.val - 1 + subTotal;
11
moves += Math.abs(result);
12

13
return result;
14
}
15
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0