Subtree Removal Game with Fibonacci Tree - Problem
Subtree Removal Game with Fibonacci Tree

Imagine a fascinating game between Alice and Bob played on a special binary tree called a Fibonacci tree. This tree has a unique recursive structure:

๐ŸŒณ Fibonacci Tree Construction:
โ€ข order(0) = empty tree
โ€ข order(1) = single node tree
โ€ข order(n) = root with left subtree order(n-2) and right subtree order(n-1)

๐ŸŽฏ Game Rules:
โ€ข Alice starts first
โ€ข On each turn, a player selects any node and removes it along with its entire subtree
โ€ข The player who is forced to remove the root node loses
โ€ข Both players play optimally

Your task: Given integer n, determine if Alice wins the game on a Fibonacci tree of order n. Return true if Alice wins, false if Bob wins.

This problem combines game theory, dynamic programming, and the beautiful mathematical properties of Fibonacci sequences!

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 1
โ€บ Output: false
๐Ÿ’ก Note: With n=1, we have a single node tree. Alice must remove the root (only node) on her first turn, so Alice loses and Bob wins.
example_2.py โ€” Small Fibonacci Tree
$ Input: n = 2
โ€บ Output: true
๐Ÿ’ก Note: With n=2, we have a root with one child. Alice can remove the child, leaving only the root for Bob to remove. Bob is forced to remove the root and loses, so Alice wins.
example_3.py โ€” Losing Position
$ Input: n = 3
โ€บ Output: false
๐Ÿ’ก Note: With n=3, we have a root with left subtree order(1) and right subtree order(2). Since 3 % 3 = 0, this is a losing position for Alice. No matter what Alice removes, Bob can force Alice into a position where she must remove the root.

Visualization

Tap to expand
Fibonacci Tree Game - Pattern DiscoveryTree Structure Evolutionn=0: โˆ…Alice losesn=1Alice losesn=2Alice winsn=3Alice losesGrundy Number Pattern Analysisn (mod 3):Grundy:Winner:Examples:00Bobn = 0,3,6,9...1โ‰ 0Alicen = 1,4,7,10...2โ‰ 0Alicen = 2,5,8,11...๐ŸŽฏ Final Formula: Alice wins โŸบ n % 3 โ‰  0Time: O(1) | Space: O(1)
Understanding the Visualization
1
Build Small Cases
Analyze Fibonacci trees for n=0,1,2,3,4,5,6... and determine winners
2
Compute Grundy Numbers
Apply Sprague-Grundy theorem to find the nim-value of each position
3
Identify Pattern
Notice that Grundy numbers follow a repeating pattern based on n mod 3
4
Apply Formula
Use the pattern: Alice wins โŸบ n % 3 โ‰  0
Key Takeaway
๐ŸŽฏ Key Insight: This game follows a beautiful mathematical pattern where Alice wins exactly when n % 3 โ‰  0, giving us an elegant O(1) solution through game theory analysis!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

Single modular arithmetic operation

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only storing the input and result

n
2n
โœ“ Linear Space

Constraints

  • 0 โ‰ค n โ‰ค 109
  • Both players play optimally
  • Alice always starts first
  • The tree structure follows the Fibonacci tree definition exactly
Asked in
Google 25 Microsoft 18 Meta 15 Amazon 12
28.4K Views
Medium Frequency
~35 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen