Class Solution
- java.lang.Object
-
- g2001_2100.s2096_step_by_step_directions_from_a_binary_tree_node_to_another.Solution
-
public class Solution extends Object
2096 - Step-By-Step Directions From a Binary Tree Node to Another.Medium
You are given the
rootof a binary tree withnnodes. Each node is uniquely assigned a value from1ton. You are also given an integerstartValuerepresenting the value of the start nodes, and a different integerdestValuerepresenting the value of the destination nodet.Find the shortest path starting from node
sand ending at nodet. Generate step-by-step directions of such path as a string consisting of only the uppercase letters'L','R', and'U'. Each letter indicates a specific direction:'L'means to go from a node to its left child node.'R'means to go from a node to its right child node.'U'means to go from a node to its parent node.
Return the step-by-step directions of the shortest path from node
sto nodet.Example 1:

Input: root = [5,1,2,3,null,6,4], startValue = 3, destValue = 6
Output: “UURL”
Explanation: The shortest path is: 3 \u2192 1 \u2192 5 \u2192 2 \u2192 6.
Example 2:

Input: root = [2,1], startValue = 2, destValue = 1
Output: “L”
Explanation: The shortest path is: 2 \u2192 1.
Constraints:
- The number of nodes in the tree is
n. 2 <= n <= 1051 <= Node.val <= n- All the values in the tree are unique.
1 <= startValue, destValue <= nstartValue != destValue
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StringgetDirections(TreeNode root, int startValue, int destValue)
-