Medium · Trees

Step-By-Step Directions From a Binary Tree Node to Another

Given the root of a binary tree whose n nodes have the distinct values 1 to n, and two different values startValue and destValue, return the shortest route from the start node to the destination node. Write the route as a string of 'L' (move to the left child), 'R' (move to the right child) and 'U' (move to the parent).

Examples

Example 1

start 3 → dest 6 = "UURL"

Output: "UURL"

Example 2

start 2 → dest 1 = "L"

Output: "L"

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Trees problems