LeetCode 1145: Binary Tree Coloring Game Solution

Master LeetCode problem 1145 (Binary Tree Coloring Game), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1145. Binary Tree Coloring Game

Problem Explanation

Explanation:

To determine if the second player can choose a node to win the game, we need to analyze the scenario where the first player colors a node and understand the impact it has on the game. The second player should choose a node such that they can control a subtree that has more nodes than the subtree controlled by the first player.

  1. We start by finding the node with value x colored by the first player and calculate the sizes of its left and right subtrees.
  2. We also calculate the size of the parent subtree (excluding the node with value x).
  3. The second player can choose the node with the largest subtree (either left, right, or parent) to ensure they have control over more nodes.
  4. If the second player can control a subtree with more nodes than the first player, they can win the game by strategically choosing the right node to color.

Time Complexity: O(n) Space Complexity: O(n) :

Solution Code

class Solution {
    public boolean btreeGameWinningMove(TreeNode root, int n, int x) {
        TreeNode xNode = findNode(root, x);
        int leftSubtree = countNodes(xNode.left);
        int rightSubtree = countNodes(xNode.right);
        int parentSubtree = n - leftSubtree - rightSubtree - 1;
        int maxSubtree = Math.max(Math.max(leftSubtree, rightSubtree), parentSubtree);
        return maxSubtree > n / 2;
    }
    
    private TreeNode findNode(TreeNode node, int val) {
        if (node == null || node.val == val) {
            return node;
        }
        TreeNode left = findNode(node.left, val);
        TreeNode right = findNode(node.right, val);
        return left != null ? left : right;
    }
    
    private int countNodes(TreeNode node) {
        if (node == null) {
            return 0;
        }
        return 1 + countNodes(node.left) + countNodes(node.right);
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1145 (Binary Tree Coloring Game)?

This page provides optimized solutions for LeetCode problem 1145 (Binary Tree Coloring Game) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1145 (Binary Tree Coloring Game)?

The time complexity for LeetCode 1145 (Binary Tree Coloring Game) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1145 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1145 in Java, C++, or Python.

Back to LeetCode Solutions