1655. Second Player Steal
Two rival crews, Red and Blue, are claiming stations in a network shaped like a binary tree with n stations, where n is odd. Station labels are exactly the distinct values 1..n, one per node. Red claims the station labelled x. Blue then claims any other station y. After that, starting with Red, the crews alternate turns: on a turn a crew claims one unclaimed station that is directly linked (parent or child) to a station that crew already holds. A crew with no legal claim simply skips its turn. When neither can move, the crew holding more stations wins.
Return true if Blue can pick some y that wins no matter how Red plays, otherwise false. Hint for efficiency: the first station removed splits the tree into at most three regions, so one traversal for subtree sizes gives O(n) time and space; do not simulate the game.
Example 1
- Input:
- root = [3,1,2,4,5,6,7], n = 7, x = 3
- Output:
- false
- Explanation:
Choosing 3 leaves its left subtree {1,4,5}, right subtree {2,6,7} and nothing above; each region has 3 nodes, never more than 3.5, so player two cannot win.
Example 2
- Input:
- root = [1,2,null,3,null,4,null,5,null], n = 5, x = 1
- Output:
- true
- Explanation:
Node 1 has a left subtree of 4 nodes, more than half of 5, so player two takes the neighbour 2 and wins the whole chain.
Constraints
1 ≤ n ≤ 1000, n is odd
the tree has n nodes whose values are exactly 1..n
1 ≤ x ≤ n
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms
Expected complexity
- Time
- O(n)
- Space
- O(n)