|
2 | 2 |
|
3 | 3 | importcom.fishercoder.common.classes.TreeNode;
|
4 | 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 | 5 | publicclass_979 {
|
58 | 6 | publicstaticclassSolution1 {
|
59 |
| -/**credit: https://leetcode.com/problems/distribute-coins-in-binary-tree/discuss/221930/JavaC%2B%2BPython-Recursive-Solution*/ |
| 7 | +/** |
| 8 | + * credit: https://leetcode.com/problems/distribute-coins-in-binary-tree/discuss/221930/JavaC%2B%2BPython-Recursive-Solution |
| 9 | + */ |
60 | 10 | intmoves =0;
|
61 | 11 |
|
62 | 12 | publicintdistributeCoins(TreeNoderoot) {
|
|