Skip to content

Commit bbd9745

Browse files
committed
Add solution #1145
1 parent 5918ce1 commit bbd9745

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,151 LeetCode solutions in JavaScript
1+
# 1,152 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -899,6 +899,7 @@
899899
1140|[Stone Game II](./solutions/1140-stone-game-ii.js)|Medium|
900900
1143|[Longest Common Subsequence](./solutions/1143-longest-common-subsequence.js)|Medium|
901901
1144|[Decrease Elements To Make Array Zigzag](./solutions/1144-decrease-elements-to-make-array-zigzag.js)|Medium|
902+
1145|[Binary Tree Coloring Game](./solutions/1145-binary-tree-coloring-game.js)|Medium|
902903
1161|[Maximum Level Sum of a Binary Tree](./solutions/1161-maximum-level-sum-of-a-binary-tree.js)|Medium|
903904
1189|[Maximum Number of Balloons](./solutions/1189-maximum-number-of-balloons.js)|Easy|
904905
1200|[Minimum Absolute Difference](./solutions/1200-minimum-absolute-difference.js)|Easy|
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* 1145. Binary Tree Coloring Game
3+
* https://leetcode.com/problems/binary-tree-coloring-game/
4+
* Difficulty: Medium
5+
*
6+
* Two players play a turn based game on a binary tree. We are given the root of this binary tree,
7+
* and the number of nodes n in the tree. n is odd, and each node has a distinct value from 1 to n.
8+
*
9+
* Initially, the first player names a value x with 1 <= x <= n, and the second player names a value
10+
* y with 1 <= y <= n and y != x. The first player colors the node with value x red, and the second
11+
* player colors the node with value y blue.
12+
*
13+
* Then, the players take turns starting with the first player. In each turn, that player chooses
14+
* a node of their color (red if player 1, blue if player 2) and colors an uncolored neighbor of
15+
* the chosen node (either the left child, right child, or parent of the chosen node.)
16+
*
17+
* If (and only if) a player cannot choose such a node in this way, they must pass their turn. If
18+
* both players pass their turn, the game ends, and the winner is the player that colored more
19+
* nodes.
20+
*
21+
* You are the second player. If it is possible to choose such a y to ensure you win the game,
22+
* return true. If it is not possible, return false.
23+
*/
24+
25+
/**
26+
* Definition for a binary tree node.
27+
* function TreeNode(val, left, right) {
28+
* this.val = (val===undefined ? 0 : val)
29+
* this.left = (left===undefined ? null : left)
30+
* this.right = (right===undefined ? null : right)
31+
* }
32+
*/
33+
/**
34+
* @param {TreeNode} root
35+
* @param {number} n
36+
* @param {number} x
37+
* @return {boolean}
38+
*/
39+
var btreeGameWinningMove = function(root, n, x) {
40+
let leftCount = 0;
41+
let rightCount = 0;
42+
43+
countNodes(root);
44+
45+
const parentCount = n - leftCount - rightCount - 1;
46+
const maxRegion = Math.max(parentCount, leftCount, rightCount);
47+
48+
return maxRegion > n / 2;
49+
50+
function countNodes(node) {
51+
if (!node) return 0;
52+
const left = countNodes(node.left);
53+
const right = countNodes(node.right);
54+
if (node.val === x) {
55+
leftCount = left;
56+
rightCount = right;
57+
}
58+
return left + right + 1;
59+
}
60+
};

0 commit comments

Comments
 (0)