You are given a recursively-defined binary tree T(order) whose shape depends only on order (not on node values). Nodes are labeled 0..N-1 using preorder traversal (root, then left subtree, then right subtree), where N = size(T(order)).
Let size(k) be the number of nodes in T(k).
T(0)
and
T(1)
each consist of a
single node
.
size(0) = 1
,
size(1) = 1
.
k >= 2
,
T(k)
has:
T(k-1)
,
T(k-2)
.
This implies:
size(k) = 1 + size(k-1) + size(k-2)
Given integers:
order
— which tree
T(order)
to use
start
— a node label in
T(order)
end
— a node label in
T(order)
Return the shortest path from start to end as a sequence of node labels, inclusive (i.e., the unique simple path in the tree).
You are not given a node/edge structure and should solve the problem using only arithmetic on integers plus order.
order: int
,
start: int
,
end: int
path: int[]
(labels from
start
to
end
, inclusive)
0 <= order
0 <= start, end < size(order)
For order = 2:
size(0)=1, size(1)=1, size(2)=3
0
, left child
1
, right child
2
If start=1, end=2, the output path is:
[1, 0, 2]
(Any equivalent representation of the same path is acceptable if the problem statement in an interview clarifies formatting.)