Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Wang-Jun-Chao committed Jul 9, 2019
1 parent 166716f commit 3f5a844
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 93 deletions.
125 changes: 49 additions & 76 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,15 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library name="JUnit4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
13 changes: 12 additions & 1 deletion [0979][Distribute Coins in Binary Tree]/src/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import org.junit.Assert;
import org.junit.Test;

/**
* @author: wangjunchao(王俊超)
* @time: 2019-07-09 21:10
**/
public class Main {
private final static Logger logger = LoggerFactory.getLogger(Main.class);
@Test
public void test1() {
TreeNode root = new TreeNode(3);
root.left = new TreeNode(0);
root.right = new TreeNode(0);

Solution solution = new Solution();
Assert.assertEquals(2, solution.distributeCoins(root));
}
}
35 changes: 19 additions & 16 deletions [0979][Distribute Coins in Binary Tree]/src/Solution.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
/**
* https://leetcode.com/problems/distribute-coins-in-binary-tree/
* @author: wangjunchao(王俊超)
* @time: 2019-07-09 18:52
**/
public class Solution {

private int moves = 0;

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


if (root == null) {
return 0;
}
Expand All @@ -30,7 +31,7 @@ public int distributeCoins(TreeNode root) {
}

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

if (node.left != null) {
distributeCoins(node.left, parent);
distributeCoins(node.left, node);
}

if (node.right != null) {
distributeCoins(node.right, parent);
distributeCoins(node.right, node);
}

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

0 comments on commit 3f5a844

Please sign in to comment.