1145. Binary Tree Coloring Game Medium
1/**
2 * [1145] Binary Tree Coloring Game
3 *
4 * 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.
5 * 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.
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 * 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.
8 * 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.
9 *
10 * Example 1:
11 * <img alt="" src="https://assets.leetcode.com/uploads/2019/08/01/1480-binary-tree-coloring-game.png" style="width: 500px; height: 310px;" />
12 * Input: root = [1,2,3,4,5,6,7,8,9,10,11], n = 11, x = 3
13 * Output: true
14 * Explanation: The second player can choose the node with value 2.
15 *
16 * Example 2:
17 *
18 * Input: root = [1,2,3], n = 3, x = 1
19 * Output: false
20 *
21 *
22 * Constraints:
23 *
24 * The number of nodes in the tree is n.
25 * 1 <= x <= n <= 100
26 * n is odd.
27 * 1 <= Node.val <= n
28 * All the values of the tree are unique.
29 *
30 */
31pub struct Solution {}
32use crate::util::tree::{TreeNode, to_tree};
33
34// problem: https://leetcode.com/problems/binary-tree-coloring-game/
35// discuss: https://leetcode.com/problems/binary-tree-coloring-game/discuss/?currentPage=1&orderBy=most_votes&query=
36
37// submission codes start here
38
39// Definition for a binary tree node.
40// #[derive(Debug, PartialEq, Eq)]
41// pub struct TreeNode {
42// pub val: i32,
43// pub left: Option<Rc<RefCell<TreeNode>>>,
44// pub right: Option<Rc<RefCell<TreeNode>>>,
45// }
46//
47// impl TreeNode {
48// #[inline]
49// pub fn new(val: i32) -> Self {
50// TreeNode {
51// val,
52// left: None,
53// right: None
54// }
55// }
56// }
57use std::rc::Rc;
58use std::cell::RefCell;
59impl Solution {
60 pub fn btree_game_winning_move(root: Option<Rc<RefCell<TreeNode>>>, n: i32, x: i32) -> bool {
61 false
62 }
63}
64
65// submission codes end
66
67#[cfg(test)]
68mod tests {
69 use super::*;
70
71 #[test]
72 fn test_1145() {
73 }
74}
75
Back
© 2025 bowen.ge All Rights Reserved.