Can the Second Tree-Coloring Player Force a Majority?

Quick Overview

Can the Second Tree-Coloring Player Force a Majority?. A binary tree has n distinct values1 through n, with n odd.

Can the Second Tree-Coloring Player Force a Majority?

Company: Anduril

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem A binary tree has n distinct values 1 through n, with n odd. The first player initially colors node x red. You choose another node y to color blue. Starting with red, players alternate coloring an uncolored parent or child of any node already their own color. A player passes only if no such move exists; play ends when both pass. Return whether some legal y guarantees blue more nodes than red. ```cpp class Solution { public: bool btreeGameWinningMove(TreeNode* root, int n, int x); }; ``` ### Examples **Example 1:** ```text Input: root = [2,1,3], n = 3, x = 1 Output: true Explanation: Choose node2. Blue then controls the two-node side containing2 and3, beyond red node1. ``` **Example 2:** ```text Input: root = [2,1,3], n = 3, x = 2 Output: false Explanation: Either legal blue seed lies in a one-node component, while red can own the other two nodes. ``` ### Constraints - The tree has exactly n nodes, with `1 <= x <= n <= 100`. - n is odd. - Node values are distinct integers from 1 through n. ### Contract considerations Retain the native TreeNode interface. Return whether a winning choice exists, not a replacement graph or seed transcript. ```hint Read the boundary condition When n is one, is there any legal second starting node distinct from x? ``` ### Boundary reminders Retain the native TreeNode interface. Return whether a winning choice exists, not a replacement graph or seed transcript.

Overview: Can the Second Tree-Coloring Player Force a Majority?. A binary tree has n distinct values1 through n, with n odd.

Anduril logo
Anduril
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

A binary tree has n distinct values 1 through n, with n odd. The first player initially colors node x red. You choose another node y to color blue. Starting with red, players alternate coloring an uncolored parent or child of any node already their own color. A player passes only if no such move exists; play ends when both pass. Return whether some legal y guarantees blue more nodes than red.

class Solution {
public:
    bool btreeGameWinningMove(TreeNode* root, int n, int x);
};

Examples

Example 1:

Input: root = [2,1,3], n = 3, x = 1
Output: true
Explanation: Choose node2. Blue then controls the two-node side containing2 and3, beyond red node1.

Example 2:

Input: root = [2,1,3], n = 3, x = 2
Output: false
Explanation: Either legal blue seed lies in a one-node component, while red can own the other two nodes.

Constraints

  • The tree has exactly n nodes, with 1 <= x <= n <= 100 .
  • n is odd.
  • Node values are distinct integers from 1 through n.

Contract considerations

Retain the native TreeNode interface. Return whether a winning choice exists, not a replacement graph or seed transcript.

Boundary reminders

Retain the native TreeNode interface. Return whether a winning choice exists, not a replacement graph or seed transcript.