Distribute Coins in Binary Tree LeetCode solution in Java

1
class Solution {
2
int count = 0;
3

4
public int helper(TreeNode root) {
5
if (root == null) return 0;
6
int left = helper(root.left);
7
int right = helper(root.right);
8
count += Math.abs(left) + Math.abs(right);
9
return (left + right + root.val - 1);
10
}
11

12
public int distributeCoins(TreeNode root) {
13
helper(root);
14
return count;
15
}
16
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0