Subtree Removal Game with Fibonacci Tree - Problem
A Fibonacci tree is a binary tree created using the order function order(n):
order(0)is the empty treeorder(1)is a binary tree with only one nodeorder(n)is a binary tree that consists of a root node with the left subtree asorder(n - 2)and the right subtree asorder(n - 1)
Alice and Bob are playing a game with a Fibonacci tree with Alice starting 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 is a tree that consists of a node in tree and all of this node's descendants. The tree could also be considered as a subtree of itself.
Input & Output
Example 1 — Small Fibonacci Tree
$
Input:
n = 1
›
Output:
true
💡 Note:
Fibonacci tree order(1) has only one node. Alice must remove it (the root) and loses. Wait, that's wrong - Alice wins because Bob is forced to remove root. Actually, with only one node, Alice removes it and wins because it's the only move.
Example 2 — Larger Tree
$
Input:
n = 3
›
Output:
false
💡 Note:
Fibonacci tree order(3) has root with left=order(1) and right=order(2). Alice can remove non-root nodes, but optimal play leads to Bob winning.
Example 3 — Pattern Example
$
Input:
n = 4
›
Output:
true
💡 Note:
Following the mathematical pattern, since 4 % 3 = 1 ≠ 0, Alice wins with optimal play.
Constraints
- 1 ≤ n ≤ 100
Visualization
Tap to expand
Understanding the Visualization
1
Input
Fibonacci tree order n
2
Game Rules
Players remove subtrees, avoiding root removal
3
Output
Winner with optimal play
Key Takeaway
🎯 Key Insight: Fibonacci tree games follow a mathematical pattern based on n % 3
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code