Skip to content

Commit 7e281d9

Browse files
add 979
1 parent 4e387c9 commit 7e281d9

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Your ideas/fixes/algorithms are more than welcome!
5050
|993|[Cousins in Binary Tree](https://leetcode.com/problems/cousins-in-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_993.java) | O(n) | O(m) (m is length of the nodes that has the max number of nodes on the same level) | |Easy| Tree, BFS
5151
|989|[Add to Array-Form of Integer](https://leetcode.com/problems/add-to-array-form-of-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_989.java) | O(max(N, logk)) | O(max(N, logk)) | |Easy| Array
5252
|985|[Sum of Even Numbers After Queries](https://leetcode.com/problems/sum-of-even-numbers-after-queries/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_985.java) | O(n) | O(n) | |Easy| Array
53+
|979|[Distribute Coins in Binary Tree](https://leetcode.com/problems/distribute-coins-in-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_979.java) | O(n) | O(1) | |Medium| Recursion
5354
|977|[Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_977.java) | O(nlogn) | O(1) | |Easy| Array
5455
|976|[Largest Perimeter Triangle](https://leetcode.com/problems/largest-perimeter-triangle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_976.java) | O(nlogn) | O(1) | |Easy| Math Array
5556
|974|[Subarray Sums Divisible by K](https://leetcode.com/problems/subarray-sums-divisible-by-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_974.java) | O(n) | O(n) | |Medium| Array|
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
/**
6+
* 979. Distribute Coins in Binary Tree
7+
*
8+
* Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there are N coins total.
9+
* In one move, we may choose two adjacent nodes and move one coin from one node to another. (The move may be from parent to child, or from child to parent.)
10+
* Return the number of moves required to make every node have exactly one coin.
11+
*
12+
* Example 1:
13+
*
14+
* 3
15+
* / \
16+
* 0 0
17+
*
18+
* Input: [3,0,0]
19+
* Output: 2
20+
* Explanation: From the root of the tree, we move one coin to its left child, and one coin to its right child.
21+
*
22+
* Example 2:
23+
*
24+
* 0
25+
* / \
26+
* 3 0
27+
*
28+
* Input: [0,3,0]
29+
* Output: 3
30+
* Explanation: From the left child of the root, we move two coins to the root [taking two moves]. Then, we move one coin from the root of the tree to the right child.
31+
*
32+
* Example 3:
33+
*
34+
* 0
35+
* / \
36+
* 1 2
37+
*
38+
* Input: [1,0,2]
39+
* Output: 2
40+
*
41+
* Example 4:
42+
*
43+
* 1
44+
* / \
45+
* 0 0
46+
* \
47+
* 3
48+
*
49+
* Input: [1,0,0,null,3]
50+
* Output: 4
51+
*
52+
*
53+
* Note:
54+
* 1<= N <= 100
55+
* 0 <= node.val <= N
56+
* */
57+
public class _979 {
58+
public static class Solution1 {
59+
/**credit: https://leetcode.com/problems/distribute-coins-in-binary-tree/discuss/221930/JavaC%2B%2BPython-Recursive-Solution*/
60+
int moves = 0;
61+
public int distributeCoins(TreeNode root) {
62+
dfs(root);
63+
return moves;
64+
}
65+
66+
int dfs(TreeNode root) {
67+
if (root == null) {
68+
return 0;
69+
}
70+
int left = dfs(root.left);
71+
int right = dfs(root.right);
72+
moves += Math.abs(left) + Math.abs(right);
73+
return root.val + left + right - 1;
74+
}
75+
}
76+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._979;
6+
import org.junit.Test;
7+
8+
import java.util.Arrays;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _979Test {
13+
private static _979.Solution1 solution1;
14+
private static TreeNode root;
15+
16+
@Test
17+
public void test1() {
18+
solution1 = new _979.Solution1();
19+
root = TreeUtils.constructBinaryTree(Arrays.asList(3, 0, 0));
20+
assertEquals(2, solution1.distributeCoins(root));
21+
}
22+
23+
@Test
24+
public void test2() {
25+
solution1 = new _979.Solution1();
26+
root = TreeUtils.constructBinaryTree(Arrays.asList(0, 3, 0));
27+
assertEquals(3, solution1.distributeCoins(root));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
solution1 = new _979.Solution1();
33+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 0, 2));
34+
assertEquals(2, solution1.distributeCoins(root));
35+
}
36+
37+
@Test
38+
public void test4() {
39+
solution1 = new _979.Solution1();
40+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 0, 0, null, 3));
41+
assertEquals(4, solution1.distributeCoins(root));
42+
}
43+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy