← DiffPush

Path from Root to Given Node

DiffPush Tier Binary Trees · Hard O(n) · O(h)

The Escalation Trail Lookup

A support org chart is stored as a binary tree of team leads, and when a ticket lands on a specific lead, the audit needs the full escalation trail — every supervisor between the chief and that lead, in order from the top. If the lead id doesn't exist in the chart, the trail comes back empty rather than guessed.

Input: A binary tree as a level-order array (null marks a missing child) and an integer target — the value of the destination node.

Output: Return the array of values on the route from root to the target node, root first. Return an empty array if the target is absent.

Constraints

Examples

Example 1

Input: {"tree":[1,2,3,4,5,null,6],"target":6}
Output: [1,3,6]
The route descends root -> right child 3 -> leaf 6.

Example 2

Input: {"tree":[1,2,3,4,5,null,6],"target":5}
Output: [1,2,5]
This time the trail bends left: root -> 2 -> 5.

Solve this in your browser →

Also on LeetCode ↗