Skip to content

Commit 61b3b3a

Browse files
authored
Create 979-distribute-coins-in-binary-tree.js
1 parent c6cf4af commit 61b3b3a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number}
12+
*/
13+
const distributeCoins = function(root) {
14+
let res = 0
15+
helper(root)
16+
return res
17+
18+
function helper(node) {
19+
if(node == null) return 0
20+
const left = helper(node.left)
21+
const right = helper(node.right)
22+
res += Math.abs(left) + Math.abs(right)
23+
return node.val + left + right - 1
24+
}
25+
};

0 commit comments

Comments
 (0)