All problems

Problem 918

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

Medium · 20 pts · Tree, Depth-First Search, String

Problem

The binary tree has distinct values. Give directions from the node with value START to the node with value DEST as a string of moves: U goes up to the parent, L goes to the left child and R to the right child. Print the shortest such string. Find the path from the root to each node as a string of L and R moves, drop their common prefix, replace what remains of the START path with Us, and append the rest of the DEST path.

Input

The first line contains START and DEST. The next two lines describe the tree: N, the number of tokens, then N level-order tokens with 'null' for missing children. All values are distinct and START != DEST.

Output

Print the directions as a string of U, L and R.

Constraints

2 <= number of nodes <= 100000; values are distinct; both values are in the tree

Example input

3 6
7
5 1 2 3 null 6 4

Example output

UURL

Explanation

In 5 1 2 3 null 6 4, go from 3 up to 1 and up to 5 (UU), then right to 2 and left to 6 (RL). The directions are UURL.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Find Duplicate Subtrees Next: Reverse Odd Levels of Binary Tree