Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3f5a844

Browse filesBrowse files
committed
commit
1 parent 166716f commit 3f5a844
Copy full SHA for 3f5a844

File tree

Expand file treeCollapse file tree

4 files changed

+90
-93
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+90
-93
lines changed

‎.idea/workspace.xml

Copy file name to clipboardExpand all lines: .idea/workspace.xml
+49-76Lines changed: 49 additions & 76 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎[0979][Distribute Coins in Binary Tree]/[0979][Distribute Coins in Binary Tree].iml

Copy file name to clipboardExpand all lines: [0979][Distribute Coins in Binary Tree]/[0979][Distribute Coins in Binary Tree].iml
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,15 @@
77
</content>
88
<orderEntry type="inheritedJdk" />
99
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module-library">
11+
<library name="JUnit4">
12+
<CLASSES>
13+
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
14+
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
15+
</CLASSES>
16+
<JAVADOC />
17+
<SOURCES />
18+
</library>
19+
</orderEntry>
1020
</component>
1121
</module>
+12-1Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
14
/**
25
* @author: wangjunchao(王俊超)
36
* @time: 2019-07-09 21:10
47
**/
58
public class Main {
6-
private final static Logger logger = LoggerFactory.getLogger(Main.class);
9+
@Test
10+
public void test1() {
11+
TreeNode root = new TreeNode(3);
12+
root.left = new TreeNode(0);
13+
root.right = new TreeNode(0);
14+
15+
Solution solution = new Solution();
16+
Assert.assertEquals(2, solution.distributeCoins(root));
17+
}
718
}

‎[0979][Distribute Coins in Binary Tree]/src/Solution.java

Copy file name to clipboardExpand all lines: [0979][Distribute Coins in Binary Tree]/src/Solution.java
+19-16Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
/**
2+
* https://leetcode.com/problems/distribute-coins-in-binary-tree/
23
* @author: wangjunchao(王俊超)
34
* @time: 2019-07-09 18:52
45
**/
56
public class Solution {
67

78
private int moves = 0;
89

10+
/**
11+
* <pre>
12+
* 解题思路:这个题目有点意思,我的方法是“借”的思想。对于任意一个叶子节点,如果val是0,
13+
* 那么表示要向其父节点取一个coin,那么parent.val -= 1, moves += 1;如果是叶子节点
14+
* 的val大于1,那么表示要给父节点val-1个coin,同时moves += (val-1)。当然这两种情况
15+
* 可以用通用的表达式:move += abs(node.val - 1), parent.val += (node.val - 1)。
16+
* 按照后序遍历的方式即可算出总的move次数。
17+
*
18+
* https://www.cnblogs.com/seyjs/p/10369614.html
19+
* </pre>
20+
* @param root
21+
* @return
22+
*/
923
public int distributeCoins(TreeNode root) {
10-
/**
11-
* <pre>
12-
* 一个节点有几种情况:
13-
* 1、没有左和右孩子
14-
* 1.1、自己的val为0,需要从上面借1个coin
15-
* 1.2、自己的val为1,不需要借coin
16-
* 1.3、自己的val>1,需要向父结点贡献 val-1个coin
17-
* 2、只有左或者右孩子
18-
* 2.1、自己的
19-
* </pre>
20-
*/
21-
22-
2324
if (root == null) {
2425
return 0;
2526
}
@@ -30,7 +31,7 @@ public int distributeCoins(TreeNode root) {
3031
}
3132

3233
/**
33-
*
34+
* 自底向上,对每一个节点,只能从父结点借,或者向父节点上交coin
3435
* @param node
3536
* @param parent
3637
*/
@@ -41,15 +42,17 @@ private void distributeCoins(TreeNode node, TreeNode parent) {
4142
}
4243

4344
if (node.left != null) {
44-
distributeCoins(node.left, parent);
45+
distributeCoins(node.left, node);
4546
}
4647

4748
if (node.right != null) {
48-
distributeCoins(node.right, parent);
49+
distributeCoins(node.right, node);
4950
}
5051

52+
// 不论向父节点借还是上交都要移动Math.abs(node.val - 1)次
5153
moves += Math.abs(node.val - 1);
5254
if (parent != null) {
55+
// 标记父结点
5356
parent.val += node.val - 1;
5457
}
5558
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.