Ctrl K

Claim More of the Tree

Medium
Two players play on a binary tree root with size nodes; size is odd, and the nodes are labelled with distinct numbers from 1 to size. First, player one claims the node with value first_pick, then player two claims any other node. After that they take turns, player one first. On a turn a player claims one unclaimed node that is adjacent to a node they already own (the neighbours of a node are its left child, right child and parent). A player with no such node skips the turn. When neither can move, whoever owns more nodes wins. You are player two. Return True if you can pick your first node so that you are sure to win, and False otherwise.
Example 1
root=tree_node([1, 2, 3, 4, 5, 6, 7]), size=7, first_pick=2 → True

By taking node 1, the second player gets the whole part of the tree above and to the right of 2 (nodes 1, 3, 6, 7): 4 nodes against 3.

Example 2
root=tree_node([1, 2, 3, 4, 5, 6, 7]), size=7, first_pick=1 → False

The root splits the tree into two halves of 3 nodes. The second player can cut off only one of them and the first player takes the other: 3 against 4.

Example 3
root=tree_node([3, 1, None, 2]), size=3, first_pick=1 → False
Constraints
  • 1 ≤ size ≤ 100, size is odd
  • the tree has exactly size nodes
  • node values are distinct numbers from 1 to size
  • 1 ≤ first_pick ≤ size
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsIs It a Search Tree? MediumTwo Mixed-Up Nodes MediumAll Leaf Paths With a Given Total MediumTree Into a Right Chain MediumDigit Branch Totals Medium
loading Python…
Input
root=tree_node([1, 2, 3, 4, 5, 6, 7]), size=7, first_pick=2
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 17 tests on the server, hidden ones included.