Skip to content

Commit d1cd6e6

Browse files
committed
Synced from java to cpp
1 parent f19547e commit d1cd6e6

8 files changed

Lines changed: 154 additions & 0 deletions

File tree

‎cpp/SUMMARY.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
* [Binary Tree Postorder Traversal](binary-tree/traversal/binary-tree-postorder-traversal.md)
7070
* [Binary Tree Level Order Traversal](binary-tree/traversal/binary-tree-level-order-traversal.md)
7171
* [Binary Tree Level Order Traversal II](binary-tree/traversal/binary-tree-level-order-traversal-ii.md)
72+
* [Invert Binary Tree](binary-tree/traversal/invert-binary-tree.md)
7273
* [Binary Search Tree Iterator](binary-tree/traversal/binary-search-tree-iterator.md)
7374
* [Binary Tree Zigzag Level Order Traversal](binary-tree/traversal/binary-tree-zigzag-level-order-traversal.md)
7475
* [Recover Binary Search Tree](binary-tree/traversal/recover-binary-search-tree.md)
@@ -187,6 +188,7 @@
187188
* [Max Points on a Line](simulation/max-points-on-a-line.md)
188189
* [Fraction to Recurring Decimal](simulation/fraction-to-recurring-decimal.md)
189190
* [Factorial Trailing Zeroes](simulation/factorial-trailing-zeroes.md)
191+
* [Nim Game](simulation/nim-game.md)
190192
* [位操作](bitwise-operations/README.md)
191193
* [Reverse Bits](bitwise-operations/reverse-bits.md)
192194
* [Repeated DNA Sequences](bitwise-operations/repeated-dna-sequences.md)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
## Invert Binary Tree
2+
3+
4+
### 描述
5+
6+
Invert a binary tree.
7+
8+
```
9+
4
10+
/ \
11+
2 7
12+
/ \ / \
13+
1 3 6 9
14+
```
15+
16+
to
17+
18+
```
19+
4
20+
/ \
21+
7 2
22+
/ \ / \
23+
9 6 3 1
24+
```
25+
26+
27+
### 分析
28+
29+
这题是大名鼎鼎的 Homebrew 的作者 Max Howell 在 Twitter 上发牢骚的那道题。原始 Tweet 地址:<https://twitter.com/mxcl/status/608682016205344768>
30+
31+
这题最简单的办法,是层次遍历,每次交换左右子树。
32+
33+
但是,这题也可以用递归解决,代码非常短。
34+
35+
36+
### 解法1 层次遍历
37+
38+
{% if book.java %}
39+
```java
40+
// Invert Binary Tree
41+
// Time Complexity: O(n), Space Complexity: O(n)
42+
public class Solution {
43+
public TreeNode invertTree(TreeNode root) {
44+
Queue<TreeNode> q = new LinkedList<>();
45+
if (root != null) q.offer(root);
46+
47+
while (!q.isEmpty()) {
48+
TreeNode node = q.poll();
49+
// swap left and right
50+
TreeNode tmp = node.left;
51+
node.left = node.right;
52+
node.right = tmp;
53+
54+
if (node.left != null) q.offer(node.left);
55+
if (node.right != null) q.offer(node.right);
56+
}
57+
return root;
58+
}
59+
}
60+
```
61+
{% endif %}
62+
63+
64+
### 解法2 递归
65+
66+
{% if book.java %}
67+
{% codesnippet "./code/invert-binary-tree-2."+book.suffix, language=book.suffix %}{% endcodesnippet %}
68+
{% endif %}

‎cpp/code/invert-binary-tree-1.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Invert Binary Tree
2+
// Time Complexity: O(n), Space Complexity: O(n)
3+
public class Solution {
4+
public TreeNode invertTree(TreeNode root) {
5+
Queue<TreeNode> q = new LinkedList<>();
6+
if (root != null) q.offer(root);
7+
8+
while (!q.isEmpty()) {
9+
TreeNode node = q.poll();
10+
// swap left and right
11+
TreeNode tmp = node.left;
12+
node.left = node.right;
13+
node.right = tmp;
14+
15+
if (node.left != null) q.offer(node.left);
16+
if (node.right != null) q.offer(node.right);
17+
}
18+
return root;
19+
}
20+
}

‎cpp/code/invert-binary-tree-2.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Invert Binary Tree
2+
// Time Complexity: O(n), Space Complexity: O(n)
3+
public class Solution {
4+
public TreeNode invertTree(TreeNode root) {
5+
if (root == null) return root;
6+
7+
TreeNode tmp = root.left;
8+
root.left = invertTree(root.right);
9+
root.right = invertTree(tmp);
10+
11+
return root;
12+
}
13+
}

‎cpp/code/nim-game.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Nim Game
2+
// Time Complexity: O(1), Space Complexity: O(1)
3+
public class Solution {
4+
public boolean canWinNim(int n) {
5+
return n % 4 != 0;
6+
}
7+
}

‎cpp/images/nim-game-1.png‎

91.6 KB
Loading

‎cpp/images/nim-game-2.png‎

4.93 KB
Loading

‎cpp/simulation/nim-game.md‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## Nim Game
2+
3+
4+
### 描述
5+
6+
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.
7+
8+
Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.
9+
10+
For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.
11+
12+
13+
### 分析
14+
15+
这题是尼姆游戏的简化版。
16+
17+
尼姆游戏最流行的版本是用12枚硬币。
18+
19+
![](../images/nim-game-1.png)
20+
21+
游戏规则很简单,游戏双方轮流取 1 枚或多枚硬币(只能在同一行),谁拿到最后一枚就算赢。
22+
23+
有趣的是,有人发现,当扩展到任意多行,每行有任意枚硬币时,利用二进制,可以把这个游戏玩得风生水起。哈佛大学的数学教授布顿在 1901 年首次发表了论文详述了这个问题,也正是他,正式将这个游戏命名为**尼姆游戏**。
24+
25+
把玩家每一步操作之后的游戏局面叫做“棋局”。在布顿的论文中,如果玩家每一步操作后的棋局能保证自己获胜,那就是“安全的”,否则就是“不安全的”。每个不安全棋局都可以一步正确的操作变成安全的,而如果没有正确地操作,一个安全的棋局就会变成不安全的。
26+
27+
回到我们上面说的那个流行版本上,可以看到在初始状态,它的二进制表示如下图:
28+
29+
![](../images/nim-game-2.png)
30+
31+
可以看到,第 2 列之和为奇数,所以这个本版的初始状态是不安全的。拿掉最上面一行的 2 枚硬币,第 1 行就变成了 1 ,从而留下了一个安全棋局。通过用其他方法试验,可以看到,拿掉第 1 行的 2 枚硬币是留下安全棋局的唯一操作。
32+
33+
尼姆游戏深受数学家喜爱并被广泛研究,它因此产生了很多变体。1910 年美国数学家穆尔就提出了一个,它规则与尼姆游戏相同,只不过玩家可以从不超过指定数 k 的任意多行里拿掉硬币。有趣的是,它同样可以通过二进制来分析,只要把安全棋局定义为:二进制表里的每列之和都可以被 k + 1 整除就可以了。
34+
35+
这题是行数为1,k=3的简化版尼姆游戏。由于是先手,只需要判断当前的石头数能否被4整除,如果能整除,则一定会输,否则一定能赢。
36+
37+
**参考资料**: <http://www.guokr.com/article/68595/>
38+
39+
40+
### 代码
41+
42+
{% if book.java %}
43+
{% codesnippet "./code/nim-game."+book.suffix, language=book.suffix %}{% endcodesnippet %}
44+
{% endif %}

0 commit comments

Comments
 (0)