#LCA0000054. Chỉ đường trên cây nhị phân (Step-By-Step Directions From a Binary Tree Node to Another)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Chỉ đường trên cây nhị phân (Step-By-Step Directions From a Binary Tree Node to Another)
Step-By-Step Directions From a Binary Tree Node to Another
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
A binary tree has distinctly indexed vertices, each with at most one left and one right child. Given a start vertex and a destination , produce directions along their shortest path:
U: move to the parent.L: move to the left child.R: move to the right child.
The direction string must list moves in their actual travel order. The original LeetCode function interface is adapted to standard PHOJ input using child indices.
Input
- The first line contains : number of vertices and root index.
- The next lines each contain : left and right child indices of vertex , with denoting an absent child.
- The last line contains distinct vertices .
Output
Print one string consisting only of U, L, and R describing the shortest path from to .
Subtasks
General constraints: ; ; ; ; các cạnh con tạo thành đúng một cây nhị phân.
-
Subtask 1 (20 points): .
-
Subtask 2 (30 points): .
-
Subtask 3 (50 points): .
Examples
Input
5 1
2 3
4 0
5 0
0 0
0 0
4 5
Output
UURL
Explanation
Starting at vertex , move U to and U again to root . Then move R to vertex and L to vertex , producing UURL.