You are given a special family of binary trees called Fibonacci trees.
Define the tree T(k) recursively:
-
T(1)
is a single node.
-
T(2)
is a single node.
-
For
k >= 3
,
T(k)
consists of a root node whose:
-
left subtree is
T(k - 1)
-
right subtree is
T(k - 2)
Let size(k) be the number of nodes in T(k):
-
size(1) = 1
-
size(2) = 1
-
size(k) = 1 + size(k - 1) + size(k - 2)
for
k >= 3
Now number all nodes of T(k) using preorder traversal (root -> left -> right) with 1-based indices from 1 to size(k).
Given:
-
an integer
k
where
1 <= k <= 60
-
two preorder indices
a
and
b
such that
1 <= a, b <= size(k)
Implement a function:
List<Long> pathInFibonacciTree(long k, long a, long b)
Return the sequence of preorder indices along the unique simple path from node a to node b in T(k).
Requirements:
-
The returned list must start with
a
and end with
b
.
-
The tree may be extremely large, so you must
not
construct the full tree explicitly in memory.
-
Your algorithm should determine the path using only the recursive structure of the Fibonacci tree.