2005. Subtree Removal Game with Fibonacci Tree

Problem Description

In this problem, Alice and Bob are playing a game with a Fibonacci tree. A Fibonacci tree is a binary tree created using the order function order(n). The game proceeds as follows:

  1. Alice starts first.
  2. On each turn, a player selects a node from the tree and removes that node along with its subtree.
  3. The player is forced to delete the root node and loses the game.

Our task is to return true if Alice wins the game or false if Bob wins, assuming both players play optimally. A subtree of a binary tree consists of a node in the tree and all of this node's descendants. The tree itself could also be considered as a subtree of itself.


Let's take n = 7.

According to the solution, when Alice starts first and plays optimally, the function findGameWinner(7) should return true, meaning Alice will win the game. The remainder of 7 % 6 is not 1, so the solution returns true for Alice's winning.

Solution Approach

The pattern in this problem is that if the number n is such that n % 6 != 1, then Alice will win. Otherwise, Bob wins. This pattern uses simple modulo arithmetic.

The solution to this problem is provided as follows using a simple Boolean function in a class called Solution.

Python Solution

3class Solution:
4    def findGameWinner(self, n: int) -> bool:
5        return n % 6 != 1

Java Solution

3public class Solution {
4    public boolean findGameWinner(int n) {
5        return n % 6 != 1;
6    }

JavaScript Solution

3class Solution {
4    findGameWinner(n) {
5        return n % 6 !== 1;
6    }

C++ Solution

3class Solution {
5    bool findGameWinner(int n) {
6        return n % 6 != 1;
7    }

C# Solution

3public class Solution {
4    public bool FindGameWinner(int n) {
5        return n % 6 != 1;
6    }


This problem is based on a simple pattern using modulo arithmetic. By following the provided solution approach and using a simple Boolean function, we can easily find out if Alice will win the game or not when she plays optimally. The solutions are provided in Python, Java, JavaScript, C++, and C# languages.Overall, understanding the pattern in the Fibonacci tree game problem is crucial to providing an optimal solution. By recognizing that if the number n is such that n % 6 != 1, Alice wins, we can quickly determine the game's outcome with minimal code and computation. This approach results in efficient solutions provided in various programming languages, including Python, Java, JavaScript, C++, and C#.

Not Sure What to Study? Take the 2-min Quiz to Find Your Missing Piece:

Depth first search is equivalent to which of the tree traversal order?

Discover Your Strengths and Weaknesses: Take Our 2-Minute Quiz to Tailor Your Study Plan:

Which algorithm is best for finding the shortest distance between two points in an unweighted graph?

Solution Implementation

Not Sure What to Study? Take the 2-min Quiz:

How many times is a tree node visited in a depth first search?

Fast Track Your Learning with Our Quick Skills Quiz:

Consider the classic dynamic programming of longest increasing subsequence:

Find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order.

For example, the length of LIS for [50, 3, 10, 7, 40, 80] is 4 and LIS is [3, 7, 40, 80].

What is the recurrence relation?

Recommended Readings

Got a question? Ask the Teaching Assistant anything you don't understand.

Still not clear? Ask in the Forum,  Discord or Submit the part you don't understand to our editors.

TA 👨‍🏫