java.lang.Object
g1101_1200.s1145_binary_tree_coloring_game.Solution

public class Solution extends Object
1145 - Binary Tree Coloring Game.<p>Medium</p> <p>Two players play a turn based game on a binary tree. We are given the <code>root</code> of this binary tree, and the number of nodes <code>n</code> in the tree. <code>n</code> is odd, and each node has a distinct value from <code>1</code> to <code>n</code>.</p> <p>Initially, the first player names a value <code>x</code> with <code>1 <= x <= n</code>, and the second player names a value <code>y</code> with <code>1 <= y <= n</code> and <code>y != x</code>. The first player colors the node with value <code>x</code> red, and the second player colors the node with value <code>y</code> blue.</p> <p>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 <strong>uncolored</strong> neighbor of the chosen node (either the left child, right child, or parent of the chosen node.)</p> <p>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.</p> <p>You are the second player. If it is possible to choose such a <code>y</code> to ensure you win the game, return <code>true</code>. If it is not possible, return <code>false</code>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2019/08/01/1480-binary-tree-coloring-game.png" alt="" /></p> <p><strong>Input:</strong> root = [1,2,3,4,5,6,7,8,9,10,11], n = 11, x = 3</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> The second player can choose the node with value 2.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> root = [1,2,3], n = 3, x = 1</p> <p><strong>Output:</strong> false</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is <code>n</code>.</li> <li><code>1 <= x <= n <= 100</code></li> <li><code>n</code> is odd.</li> <li>1 <= Node.val <= n</li> <li>All the values of the tree are <strong>unique</strong>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • btreeGameWinningMove

      public boolean btreeGameWinningMove(TreeNode root, int n, int x)