A Fibonacci tree is a binary tree created using the order function order(n):
order(0) is the empty tree.order(1) is a binary tree with only one node.order(n) is a binary tree that consists of a root node with the left subtree as order(n - 2) and the right subtree as order(n - 1).Alice and Bob are playing a game with a Fibonacci tree with Alice staring first. On each turn, a player selects a node and removes that node and its subtree. The player that is forced to delete root loses.
Given the integer n, return true if Alice wins the game or false if Bob wins, assuming both players play optimally.
A subtree of a binary tree tree is a tree that consists of a node in tree and all of this node's descendants. The tree tree could also be considered as a subtree of itself.
Example 1:

Input: n = 3 Output: true Explanation: Alice takes the node 1 in the right subtree. Bob takes either the 1 in the left subtree or the 2 in the right subtree. Alice takes whichever node Bob doesn't take. Bob is forced to take the root node 3, so Bob will lose. Return true because Alice wins.
Example 2:

Input: n = 1 Output: false Explanation: Alice is forced to take the root node 1, so Alice will lose. Return false because Alice loses.
Example 3:

Input: n = 2 Output: true Explanation: Alice takes the node 1. Bob is forced to take the root node 2, so Bob will lose. Return true because Alice wins.
Constraints:
1 <= n <= 100Problem Overview: You are given a tree where players take turns removing valid subtrees. A move is allowed only when the removed subtree size follows Fibonacci constraints derived from a Fibonacci tree structure. The goal is to determine whether the first player has a winning strategy assuming both players play optimally.
Approach 1: Brute Force Game Simulation (Exponential Time, O(n) space)
The most direct idea is to simulate every possible move. At each turn, iterate over all nodes and try removing any subtree whose size forms a valid Fibonacci segment of the current tree. After removing a subtree, recursively evaluate the remaining game state and check if the opponent loses. This essentially explores the full game tree and evaluates win/lose states. The approach quickly becomes infeasible because the number of possible subtree removals grows rapidly, leading to exponential time complexity while using O(n) recursion stack space.
Approach 2: DFS with Fibonacci Decomposition and Game DP (O(n) time, O(n) space)
The optimal strategy relies on properties of Fibonacci trees. Precompute Fibonacci numbers up to n and check whether the tree size belongs to the sequence. Then run a DFS to compute subtree sizes for every node. If the tree size equals F[k], any valid move must split the tree into components of size F[k-1] and F[k-2]. During DFS, track edges where removing that edge creates exactly these two Fibonacci-sized components.
Once such a split is found, recursively evaluate both resulting components as independent games. Use memoization to cache results for subtree sizes to avoid recomputation. If any valid split leads to a position where the opponent loses, the current state is winning. The algorithm combines subtree size computation from tree traversal with memoized decisions from dynamic programming and winning-state evaluation from game theory. Because each node and edge is processed a constant number of times, the total runtime stays O(n) with O(n) auxiliary space.
Recommended for interviews: Interviewers expect the Fibonacci decomposition insight combined with DFS subtree size computation. Starting with brute force demonstrates understanding of the game state, but the optimized approach shows mastery of tree recursion, Fibonacci constraints, and dynamic programming for impartial games.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Game Simulation | Exponential | O(n) | Conceptual understanding of all possible subtree removals |
| DFS with Fibonacci Decomposition + Game DP | O(n) | O(n) | Optimal approach for large trees using Fibonacci structure |
LeetCode 2005: Subtree Removal Game with Fibonacci Tree • AlitaCode • 6 views views
Practice Subtree Removal Game with Fibonacci Tree with our built-in code editor and test cases.
Practice on FleetCode