277. Subtree Removal Game with Fibonacci Tree

A Fibonacci tree is a special type of binary tree constructed using the following recursive function:

  • order(0) is an empty tree.
  • order(1) is a binary tree with a single node.
  • order(n) is a binary tree where the root node has: A left subtree of order(n - 2) and A right subtree of order(n - 1).

Alice and Bob are playing a game with a Fibonacci tree of order(n), where Alice goes first. On each turn, a player selects a node and removes that node along with its entire subtree. The player forced to delete the root node loses.

Given an integer n, return true if Alice wins the game, or false if Bob wins, assuming both play optimally.

A subtree of a binary tree consists of a node and all its descendants. The entire tree can also be considered as a subtree of itself.

Example 1:

Input: n = 3

Output: true

Explanation:

The Fibonacci tree order(3) is formed as follows:

Alice's move: She removes node 1 in the right subtree.

Bob's move: He has two choices:

Remove node 1 in the left subtree

Remove node 2 in the right subtree

Alice's move: She removes whichever node Bob didn’t choose.

Bob is forced to delete the root node (3) and loses.

Thus, Alice wins, so the output is true.

Example 2:

Input: n = 1

Output: false

Explanation:

The Fibonacci tree order(1) contains only the root node (1).

Alice is forced to delete the root node on her first move, so she loses immediately.

Thus, Bob wins, so the output is false.

Now Your Turn!

Pick the correct output for the given input

Input: n = 2

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n <= 100

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
bool findGameWinner(const int n) {
}
};
Test Case

Input:

N