Skip to content

Commit e73d846

Browse files
committed
+ problem 1145
1 parent 82b1a70 commit e73d846

File tree

5 files changed

+222
-0
lines changed

5 files changed

+222
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# 1145. Binary Tree Coloring Game
2+
Two players play a turn based game on a binary tree. We are given the `root` of this binary tree, and the number of nodes `n` in the tree. `n` is odd, and each node has a distinct value from `1` to `n`.
3+
4+
Initially, the first player names a value `x` with `1 <= x <= n`, and the second player names a value `y` with `1 <= y <= n` and `y != x`. The first player colors the node with value `x` red, and the second player colors the node with value `y` blue.
5+
6+
Then, the players take turns starting with the first player. In each turn, that player chooses a node of their color (red if player 1, blue if player 2) and colors an **uncolored** neighbor of the chosen node (either the left child, right child, or parent of the chosen node.)
7+
8+
If (and only if) a player cannot choose such a node in this way, they must pass their turn. If both players pass their turn, the game ends, and the winner is the player that colored more nodes.
9+
10+
You are the second player. If it is possible to choose such a `y` to ensure you win the game, return `true`. If it is not possible, return `false`.
11+
12+
#### Example 1:
13+
![](https://assets.leetcode.com/uploads/2019/08/01/1480-binary-tree-coloring-game.png)
14+
<pre>
15+
<strong>Input:</strong> root = [1,2,3,4,5,6,7,8,9,10,11], n = 11, x = 3
16+
<strong>Output:</strong> true
17+
<strong>Explanation:</strong> The second player can choose the node with value 2.
18+
</pre>
19+
20+
#### Example 1:
21+
<pre>
22+
<strong>Input:</strong> root = [1,2,3], n = 3, x = 1
23+
<strong>Output:</strong> false
24+
</pre>
25+
26+
#### Constraints:
27+
* The number of nodes in the tree is `n`.
28+
* `1 <= x <= n <= 100`
29+
* `n` is odd.
30+
* 1 <= Node.val <= n
31+
* All the values of the tree are **unique**.
32+
33+
## Solutions (Python)
34+
35+
### 1. Solution
36+
```Python
37+
# Definition for a binary tree node.
38+
# class TreeNode:
39+
# def __init__(self, val=0, left=None, right=None):
40+
# self.val = val
41+
# self.left = left
42+
# self.right = right
43+
class Solution:
44+
def btreeGameWinningMove(self, root: Optional[TreeNode], n: int, x: int) -> bool:
45+
nodes = [root]
46+
nodex = root
47+
48+
while nodes != []:
49+
curr = nodes.pop()
50+
51+
if curr.left is not None:
52+
if curr.left.val == x:
53+
nodex = curr.left
54+
break
55+
nodes.append(curr.left)
56+
if curr.right is not None:
57+
if curr.right.val == x:
58+
nodex = curr.right
59+
break
60+
nodes.append(curr.right)
61+
62+
for node in [root, nodex.left, nodex.right]:
63+
if node is None or node.val == x:
64+
continue
65+
66+
nodes = [node]
67+
count = 0
68+
69+
while nodes != []:
70+
curr = nodes.pop()
71+
count += 1
72+
73+
if curr.left is not None and curr.left.val != x:
74+
nodes.append(curr.left)
75+
if curr.right is not None and curr.right.val != x:
76+
nodes.append(curr.right)
77+
78+
if count > n // 2:
79+
return True
80+
81+
return False
82+
```
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# 1145. 二叉树着色游戏
2+
有两位极客玩家参与了一场「二叉树着色」的游戏。游戏中,给出二叉树的根节点 `root`,树上总共有 `n` 个节点,且 `n` 为奇数,其中每个节点上的值从 `1``n` 各不相同。
3+
4+
最开始时:
5+
6+
* 「一号」玩家从 `[1, n]` 中取一个值 `x``1 <= x <= n`);
7+
* 「二号」玩家也从 `[1, n]` 中取一个值 `y``1 <= y <= n`)且 `y != x`
8+
9+
「一号」玩家给值为 `x` 的节点染上红色,而「二号」玩家给值为 `y` 的节点染上蓝色。
10+
11+
之后两位玩家轮流进行操作,「一号」玩家先手。每一回合,玩家选择一个被他染过色的节点,将所选节点一个 **未着色** 的邻节点(即左右子节点、或父节点)进行染色(「一号」玩家染红色,「二号」玩家染蓝色)。
12+
13+
如果(且仅在此种情况下)当前玩家无法找到这样的节点来染色时,其回合就会被跳过。
14+
15+
若两个玩家都没有可以染色的节点时,游戏结束。着色节点最多的那位玩家获得胜利。
16+
17+
现在,假设你是「二号」玩家,根据所给出的输入,假如存在一个 `y` 值可以确保你赢得这场游戏,则返回 `true` ;若无法获胜,就请返回 `false`
18+
19+
#### 示例 1:
20+
![](https://assets.leetcode.com/uploads/2019/08/01/1480-binary-tree-coloring-game.png)
21+
<pre>
22+
<strong>输入:</strong> root = [1,2,3,4,5,6,7,8,9,10,11], n = 11, x = 3
23+
<strong>输出:</strong> true
24+
<strong>解释:</strong> 第二个玩家可以选择值为 2 的节点。
25+
</pre>
26+
27+
#### 示例 1:
28+
<pre>
29+
<strong>输入:</strong> root = [1,2,3], n = 3, x = 1
30+
<strong>输出:</strong> false
31+
</pre>
32+
33+
#### 提示:
34+
* 树中节点数目为 `n`
35+
* `1 <= x <= n <= 100`
36+
* `n` 是奇数
37+
* `1 <= Node.val <= n`
38+
* 树中所有值 **互不相同**
39+
40+
## 题解 (Python)
41+
42+
### 1. 题解
43+
```Python
44+
# Definition for a binary tree node.
45+
# class TreeNode:
46+
# def __init__(self, val=0, left=None, right=None):
47+
# self.val = val
48+
# self.left = left
49+
# self.right = right
50+
class Solution:
51+
def btreeGameWinningMove(self, root: Optional[TreeNode], n: int, x: int) -> bool:
52+
nodes = [root]
53+
nodex = root
54+
55+
while nodes != []:
56+
curr = nodes.pop()
57+
58+
if curr.left is not None:
59+
if curr.left.val == x:
60+
nodex = curr.left
61+
break
62+
nodes.append(curr.left)
63+
if curr.right is not None:
64+
if curr.right.val == x:
65+
nodex = curr.right
66+
break
67+
nodes.append(curr.right)
68+
69+
for node in [root, nodex.left, nodex.right]:
70+
if node is None or node.val == x:
71+
continue
72+
73+
nodes = [node]
74+
count = 0
75+
76+
while nodes != []:
77+
curr = nodes.pop()
78+
count += 1
79+
80+
if curr.left is not None and curr.left.val != x:
81+
nodes.append(curr.left)
82+
if curr.right is not None and curr.right.val != x:
83+
nodes.append(curr.right)
84+
85+
if count > n // 2:
86+
return True
87+
88+
return False
89+
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def btreeGameWinningMove(self, root: Optional[TreeNode], n: int, x: int) -> bool:
9+
nodes = [root]
10+
nodex = root
11+
12+
while nodes != []:
13+
curr = nodes.pop()
14+
15+
if curr.left is not None:
16+
if curr.left.val == x:
17+
nodex = curr.left
18+
break
19+
nodes.append(curr.left)
20+
if curr.right is not None:
21+
if curr.right.val == x:
22+
nodex = curr.right
23+
break
24+
nodes.append(curr.right)
25+
26+
for node in [root, nodex.left, nodex.right]:
27+
if node is None or node.val == x:
28+
continue
29+
30+
nodes = [node]
31+
count = 0
32+
33+
while nodes != []:
34+
curr = nodes.pop()
35+
count += 1
36+
37+
if curr.left is not None and curr.left.val != x:
38+
nodes.append(curr.left)
39+
if curr.right is not None and curr.right.val != x:
40+
nodes.append(curr.right)
41+
42+
if count > n // 2:
43+
return True
44+
45+
return False

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@
624624
[1139][1139l]|[Largest 1-Bordered Square][1139] |![rs]
625625
[1143][1143l]|[Longest Common Subsequence][1143] |![rs]
626626
[1144][1144l]|[Decrease Elements To Make Array Zigzag][1144] |![rs]
627+
[1145][1145l]|[Binary Tree Coloring Game][1145] |![py]
627628
[1146][1146l]|[Snapshot Array][1146] |![rb]&nbsp;&nbsp;![rs]
628629
[1154][1154l]|[Day of the Year][1154] |![rs]
629630
[1155][1155l]|[Number of Dice Rolls With Target Sum][1155] |![py]&nbsp;&nbsp;![rs]
@@ -1902,6 +1903,7 @@
19021903
[1139]:Problemset/1139-Largest%201-Bordered%20Square/README.md#1139-largest-1-bordered-square
19031904
[1143]:Problemset/1143-Longest%20Common%20Subsequence/README.md#1143-longest-common-subsequence
19041905
[1144]:Problemset/1144-Decrease%20Elements%20To%20Make%20Array%20Zigzag/README.md#1144-decrease-elements-to-make-array-zigzag
1906+
[1145]:Problemset/1145-Binary%20Tree%20Coloring%20Game/README.md#1145-binary-tree-coloring-game
19051907
[1146]:Problemset/1146-Snapshot%20Array/README.md#1146-snapshot-array
19061908
[1154]:Problemset/1154-Day%20of%20the%20Year/README.md#1154-day-of-the-year
19071909
[1155]:Problemset/1155-Number%20of%20Dice%20Rolls%20With%20Target%20Sum/README.md#1155-number-of-dice-rolls-with-target-sum
@@ -3183,6 +3185,7 @@
31833185
[1139l]:https://leetcode.com/problems/largest-1-bordered-square/
31843186
[1143l]:https://leetcode.com/problems/longest-common-subsequence/
31853187
[1144l]:https://leetcode.com/problems/decrease-elements-to-make-array-zigzag/
3188+
[1145l]:https://leetcode.com/problems/binary-tree-coloring-game/
31863189
[1146l]:https://leetcode.com/problems/snapshot-array/
31873190
[1154l]:https://leetcode.com/problems/day-of-the-year/
31883191
[1155l]:https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@
624624
[1139][1139l]|[最大的以 1 为边界的正方形][1139] |![rs]
625625
[1143][1143l]|[最长公共子序列][1143] |![rs]
626626
[1144][1144l]|[递减元素使数组呈锯齿状][1144] |![rs]
627+
[1145][1145l]|[二叉树着色游戏][1145] |![py]
627628
[1146][1146l]|[快照数组][1146] |![rb]&nbsp;&nbsp;![rs]
628629
[1154][1154l]|[一年中的第几天][1154] |![rs]
629630
[1155][1155l]|[掷骰子的N种方法][1155] |![py]&nbsp;&nbsp;![rs]
@@ -1902,6 +1903,7 @@
19021903
[1139]:Problemset/1139-Largest%201-Bordered%20Square/README_CN.md#1139-最大的以-1-为边界的正方形
19031904
[1143]:Problemset/1143-Longest%20Common%20Subsequence/README_CN.md#1143-最长公共子序列
19041905
[1144]:Problemset/1144-Decrease%20Elements%20To%20Make%20Array%20Zigzag/README_CN.md#1144-递减元素使数组呈锯齿状
1906+
[1145]:Problemset/1145-Binary%20Tree%20Coloring%20Game/README_CN.md#1145-二叉树着色游戏
19051907
[1146]:Problemset/1146-Snapshot%20Array/README_CN.md#1146-快照数组
19061908
[1154]:Problemset/1154-Day%20of%20the%20Year/README_CN.md#1154-一年中的第几天
19071909
[1155]:Problemset/1155-Number%20of%20Dice%20Rolls%20With%20Target%20Sum/README_CN.md#1155-掷骰子的n种方法
@@ -3183,6 +3185,7 @@
31833185
[1139l]:https://leetcode.cn/problems/largest-1-bordered-square/
31843186
[1143l]:https://leetcode.cn/problems/longest-common-subsequence/
31853187
[1144l]:https://leetcode.cn/problems/decrease-elements-to-make-array-zigzag/
3188+
[1145l]:https://leetcode.cn/problems/binary-tree-coloring-game/
31863189
[1146l]:https://leetcode.cn/problems/snapshot-array/
31873190
[1154l]:https://leetcode.cn/problems/day-of-the-year/
31883191
[1155l]:https://leetcode.cn/problems/number-of-dice-rolls-with-target-sum/

0 commit comments

Comments
 (0)