This question evaluates understanding of tree data structures, traversal and path reconstruction techniques, including reasoning about lowest common ancestors, ancestor–descendant relationships, duplicate identification, and handling absent targets.
Given a binary tree and two target nodes a and b, return the simple path from a to b (inclusive) as an ordered list of node values.
a
to
b
along parent/child links.
val
,
left
, and
right
.
Tree:
1
/ \
2 3
Input: a = 2, b = 3
Output: [2, 1, 3]
Tree:
1
/ \
2 3
/ \
6 5
/ \
7 8
Input: a = 3, b = 5
Output: [3, 1, 2, 5]
a
and
b
(e.g., node references/pointers, unique IDs, or values).
a
and
b
?