forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_979.java
More file actions
27 lines (23 loc) · 740 Bytes
/
_979.java
File metadata and controls
27 lines (23 loc) · 740 Bytes
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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
public class _979 {
public static class Solution1 {
/**
* credit: https://leetcode.com/problems/distribute-coins-in-binary-tree/discuss/221930/JavaC%2B%2BPython-Recursive-Solution
*/
int moves = 0;
public int distributeCoins(TreeNode root) {
dfs(root);
return moves;
}
int dfs(TreeNode root) {
if (root == null) {
return 0;
}
int left = dfs(root.left);
int right = dfs(root.right);
moves += Math.abs(left) + Math.abs(right);
return root.val + left + right - 1;
}
}
}