Skip to content

Commit 5d5c697

Browse files
committed
feat: add solutions to lc problem: No.1145
No.1145.Binary Tree Coloring Game
1 parent 666606c commit 5d5c697

File tree

8 files changed

+316
-91
lines changed

8 files changed

+316
-91
lines changed

solution/1100-1199/1145.Binary Tree Coloring Game/README.md

Lines changed: 108 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@
5858

5959
**方法一:DFS**
6060

61-
先通过 $DFS$,找到 $x$ 所在的节点,我们记为 $node$。然后统计 $node$ 的左子树、右子树、父节点方向上的节点个数。如果这三个方向上有任何一个节点个数超过了节点总数的一半,则存在一个必胜策略
61+
我们先通过 $DFS$,找到「一号」玩家着色点 $x$ 所在的节点,记为 $node$。
6262

63-
这里 $node$ 父节点方向上的节点个数,可以由节点总数 $n$ 减去 $node$ 及其 $node$ 的左右子树节点数之和得到
63+
接下来,我们统计 $node$ 的左子树、右子树的节点个数,分别记为 $l$ 和 $r$,而 $node$ 父节点方向上的个数为 $n - l - r - 1$。只要满足 $\max(l, r, n - l - r - 1) > \frac{n}{2}$,则「二号」玩家存在一个必胜策略
6464

6565
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是节点总数。
6666

@@ -91,9 +91,7 @@ class Solution:
9191

9292
node = dfs(root)
9393
l, r = count(node.left), count(node.right)
94-
t = n - l - r - 1
95-
m = max(l, r, t)
96-
return m > n - m
94+
return max(l, r, n - l - r - 1) > n // 2
9795
```
9896

9997
### **Java**
@@ -121,26 +119,22 @@ class Solution {
121119
TreeNode node = dfs(root, x);
122120
int l = count(node.left);
123121
int r = count(node.right);
124-
int m = Math.max(Math.max(l, r), n - l - r - 1);
125-
return m > n - m;
126-
}
127-
128-
private int count(TreeNode node) {
129-
if (node == null) {
130-
return 0;
131-
}
132-
return 1 + count(node.left) + count(node.right);
122+
return Math.max(Math.max(l, r), n - l - r - 1) > n / 2;
133123
}
134124

135125
private TreeNode dfs(TreeNode root, int x) {
136126
if (root == null || root.val == x) {
137127
return root;
138128
}
139-
TreeNode l = dfs(root.left, x);
140-
if (l != null) {
141-
return l;
129+
TreeNode node = dfs(root.left, x);
130+
return node == null ? dfs(root.right, x) : node;
131+
}
132+
133+
private int count(TreeNode root) {
134+
if (root == null) {
135+
return 0;
142136
}
143-
return dfs(root.right, x);
137+
return 1 + count(root.left) + count(root.right);
144138
}
145139
}
146140
```
@@ -162,22 +156,24 @@ class Solution {
162156
class Solution {
163157
public:
164158
bool btreeGameWinningMove(TreeNode* root, int n, int x) {
165-
TreeNode* node = dfs(root, x);
166-
int l = count(node->left);
167-
int r = count(node->right);
168-
int m = max(max(l, r), n - l - r - 1);
169-
return m > n - m;
159+
auto node = dfs(root, x);
160+
int l = count(node->left), r = count(node->right);
161+
return max({l, r, n - l - r - 1}) > n / 2;
170162
}
171163

172-
int count(TreeNode* root) {
173-
if (!root) return 0;
174-
return 1 + count(root->left) + count(root->right);
164+
TreeNode* dfs(TreeNode* root, int x) {
165+
if (!root || root->val == x) {
166+
return root;
167+
}
168+
auto node = dfs(root->left, x);
169+
return node ? node : dfs(root->right, x);
175170
}
176171

177-
TreeNode* dfs(TreeNode* root, int x) {
178-
if (!root || root->val == x) return root;
179-
auto l = dfs(root->left, x);
180-
return l ? l : dfs(root->right, x);
172+
int count(TreeNode* root) {
173+
if (!root) {
174+
return 0;
175+
}
176+
return 1 + count(root->left) + count(root->right);
181177
}
182178
};
183179
```
@@ -226,6 +222,88 @@ func max(a, b int) int {
226222
}
227223
```
228224

225+
### **JavaScript**
226+
227+
```js
228+
/**
229+
* Definition for a binary tree node.
230+
* function TreeNode(val, left, right) {
231+
* this.val = (val===undefined ? 0 : val)
232+
* this.left = (left===undefined ? null : left)
233+
* this.right = (right===undefined ? null : right)
234+
* }
235+
*/
236+
/**
237+
* @param {TreeNode} root
238+
* @param {number} n
239+
* @param {number} x
240+
* @return {boolean}
241+
*/
242+
var btreeGameWinningMove = function (root, n, x) {
243+
const dfs = root => {
244+
if (!root || root.val === x) {
245+
return root;
246+
}
247+
return dfs(root.left) || dfs(root.right);
248+
};
249+
250+
const count = root => {
251+
if (!root) {
252+
return 0;
253+
}
254+
return 1 + count(root.left) + count(root.right);
255+
};
256+
257+
const node = dfs(root);
258+
const l = count(node.left);
259+
const r = count(node.right);
260+
return Math.max(l, r, n - l - r - 1) > n / 2;
261+
};
262+
```
263+
264+
### **TypeScript**
265+
266+
```ts
267+
/**
268+
* Definition for a binary tree node.
269+
* class TreeNode {
270+
* val: number
271+
* left: TreeNode | null
272+
* right: TreeNode | null
273+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
274+
* this.val = (val===undefined ? 0 : val)
275+
* this.left = (left===undefined ? null : left)
276+
* this.right = (right===undefined ? null : right)
277+
* }
278+
* }
279+
*/
280+
281+
function btreeGameWinningMove(
282+
root: TreeNode | null,
283+
n: number,
284+
x: number,
285+
): boolean {
286+
const dfs = (root: TreeNode | null): TreeNode | null => {
287+
if (!root || root.val === x) {
288+
return root;
289+
}
290+
return dfs(root.left) || dfs(root.right);
291+
};
292+
293+
const count = (root: TreeNode | null): number => {
294+
if (!root) {
295+
return 0;
296+
}
297+
return 1 + count(root.left) + count(root.right);
298+
};
299+
300+
const node = dfs(root);
301+
const l = count(node.left);
302+
const r = count(node.right);
303+
return Math.max(l, r, n - l - r - 1) > n / 2;
304+
}
305+
```
306+
229307
### **...**
230308

231309
```

solution/1100-1199/1145.Binary Tree Coloring Game/README_EN.md

Lines changed: 106 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ class Solution:
6868

6969
node = dfs(root)
7070
l, r = count(node.left), count(node.right)
71-
t = n - l - r - 1
72-
m = max(l, r, t)
73-
return m > n - m
71+
return max(l, r, n - l - r - 1) > n // 2
7472
```
7573

7674
### **Java**
@@ -96,26 +94,22 @@ class Solution {
9694
TreeNode node = dfs(root, x);
9795
int l = count(node.left);
9896
int r = count(node.right);
99-
int m = Math.max(Math.max(l, r), n - l - r - 1);
100-
return m > n - m;
101-
}
102-
103-
private int count(TreeNode node) {
104-
if (node == null) {
105-
return 0;
106-
}
107-
return 1 + count(node.left) + count(node.right);
97+
return Math.max(Math.max(l, r), n - l - r - 1) > n / 2;
10898
}
10999

110100
private TreeNode dfs(TreeNode root, int x) {
111101
if (root == null || root.val == x) {
112102
return root;
113103
}
114-
TreeNode l = dfs(root.left, x);
115-
if (l != null) {
116-
return l;
104+
TreeNode node = dfs(root.left, x);
105+
return node == null ? dfs(root.right, x) : node;
106+
}
107+
108+
private int count(TreeNode root) {
109+
if (root == null) {
110+
return 0;
117111
}
118-
return dfs(root.right, x);
112+
return 1 + count(root.left) + count(root.right);
119113
}
120114
}
121115
```
@@ -137,22 +131,24 @@ class Solution {
137131
class Solution {
138132
public:
139133
bool btreeGameWinningMove(TreeNode* root, int n, int x) {
140-
TreeNode* node = dfs(root, x);
141-
int l = count(node->left);
142-
int r = count(node->right);
143-
int m = max(max(l, r), n - l - r - 1);
144-
return m > n - m;
134+
auto node = dfs(root, x);
135+
int l = count(node->left), r = count(node->right);
136+
return max({l, r, n - l - r - 1}) > n / 2;
145137
}
146138

147-
int count(TreeNode* root) {
148-
if (!root) return 0;
149-
return 1 + count(root->left) + count(root->right);
139+
TreeNode* dfs(TreeNode* root, int x) {
140+
if (!root || root->val == x) {
141+
return root;
142+
}
143+
auto node = dfs(root->left, x);
144+
return node ? node : dfs(root->right, x);
150145
}
151146

152-
TreeNode* dfs(TreeNode* root, int x) {
153-
if (!root || root->val == x) return root;
154-
auto l = dfs(root->left, x);
155-
return l ? l : dfs(root->right, x);
147+
int count(TreeNode* root) {
148+
if (!root) {
149+
return 0;
150+
}
151+
return 1 + count(root->left) + count(root->right);
156152
}
157153
};
158154
```
@@ -201,6 +197,88 @@ func max(a, b int) int {
201197
}
202198
```
203199

200+
### **JavaScript**
201+
202+
```js
203+
/**
204+
* Definition for a binary tree node.
205+
* function TreeNode(val, left, right) {
206+
* this.val = (val===undefined ? 0 : val)
207+
* this.left = (left===undefined ? null : left)
208+
* this.right = (right===undefined ? null : right)
209+
* }
210+
*/
211+
/**
212+
* @param {TreeNode} root
213+
* @param {number} n
214+
* @param {number} x
215+
* @return {boolean}
216+
*/
217+
var btreeGameWinningMove = function (root, n, x) {
218+
const dfs = root => {
219+
if (!root || root.val === x) {
220+
return root;
221+
}
222+
return dfs(root.left) || dfs(root.right);
223+
};
224+
225+
const count = root => {
226+
if (!root) {
227+
return 0;
228+
}
229+
return 1 + count(root.left) + count(root.right);
230+
};
231+
232+
const node = dfs(root);
233+
const l = count(node.left);
234+
const r = count(node.right);
235+
return Math.max(l, r, n - l - r - 1) > n / 2;
236+
};
237+
```
238+
239+
### **TypeScript**
240+
241+
```ts
242+
/**
243+
* Definition for a binary tree node.
244+
* class TreeNode {
245+
* val: number
246+
* left: TreeNode | null
247+
* right: TreeNode | null
248+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
249+
* this.val = (val===undefined ? 0 : val)
250+
* this.left = (left===undefined ? null : left)
251+
* this.right = (right===undefined ? null : right)
252+
* }
253+
* }
254+
*/
255+
256+
function btreeGameWinningMove(
257+
root: TreeNode | null,
258+
n: number,
259+
x: number,
260+
): boolean {
261+
const dfs = (root: TreeNode | null): TreeNode | null => {
262+
if (!root || root.val === x) {
263+
return root;
264+
}
265+
return dfs(root.left) || dfs(root.right);
266+
};
267+
268+
const count = (root: TreeNode | null): number => {
269+
if (!root) {
270+
return 0;
271+
}
272+
return 1 + count(root.left) + count(root.right);
273+
};
274+
275+
const node = dfs(root);
276+
const l = count(node.left);
277+
const r = count(node.right);
278+
return Math.max(l, r, n - l - r - 1) > n / 2;
279+
}
280+
```
281+
204282
### **...**
205283

206284
```

solution/1100-1199/1145.Binary Tree Coloring Game/Solution.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,23 @@
1212
class Solution {
1313
public:
1414
bool btreeGameWinningMove(TreeNode* root, int n, int x) {
15-
TreeNode* node = dfs(root, x);
16-
int l = count(node->left);
17-
int r = count(node->right);
18-
int m = max(max(l, r), n - l - r - 1);
19-
return m > n - m;
15+
auto node = dfs(root, x);
16+
int l = count(node->left), r = count(node->right);
17+
return max({l, r, n - l - r - 1}) > n / 2;
2018
}
2119

22-
int count(TreeNode* root) {
23-
if (!root) return 0;
24-
return 1 + count(root->left) + count(root->right);
20+
TreeNode* dfs(TreeNode* root, int x) {
21+
if (!root || root->val == x) {
22+
return root;
23+
}
24+
auto node = dfs(root->left, x);
25+
return node ? node : dfs(root->right, x);
2526
}
2627

27-
TreeNode* dfs(TreeNode* root, int x) {
28-
if (!root || root->val == x) return root;
29-
auto l = dfs(root->left, x);
30-
return l ? l : dfs(root->right, x);
28+
int count(TreeNode* root) {
29+
if (!root) {
30+
return 0;
31+
}
32+
return 1 + count(root->left) + count(root->right);
3133
}
3234
};

0 commit comments

Comments
 (0)