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

You are given the root of a binary tree with n nodes. Each node is uniquely assigned a value from 1 to n. You are also given an integer startValue representing the value of the start node s, and a different integer destValue representing the value of the destination node t.

Find the shortest path starting from node s and ending at node t. 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 s to node t.

Input & Output

Example 1 — Basic Tree Navigation
$ Input: root = [5,1,2,3,null,6,4], startValue = 3, destValue = 6
Output: UURL
💡 Note: Start at node 3, go up to node 1, up to root node 5, then right to node 2, then left to reach node 6. Path: 3→1→5→2→6 gives directions UURL.
Example 2 — Sibling Nodes
$ Input: root = [2,1,3], startValue = 3, destValue = 1
Output: UL
💡 Note: Start at node 3 (right child), go up to root node 2, then left to node 1. Simple sibling traversal: 3→2→1 gives UL.
Example 3 — Same Subtree
$ Input: root = [5,1,2,3,null,6,4], startValue = 3, destValue = 4
Output: UURL
💡 Note: From node 3 to node 4: go up to node 1, then up to root 5, then right to node 2, then right to node 4. Path gives UURL.

Constraints

  • The number of nodes in the tree is n.
  • 2 ≤ n ≤ 105
  • 1 ≤ Node.val ≤ n
  • All values in the tree are unique.
  • startValue ≠ destValue

Visualization

Tap to expand
Binary Tree Path: Start to Destination INPUT Binary Tree Structure 5 1 2 3 START 6 DEST 4 Input Values: root = [5,1,2,3,null,6,4] startValue = 3 destValue = 6 = Start = Destination ALGORITHM STEPS 1 Find LCA Lowest Common Ancestor of nodes 3 and 6 = node 5 2 Path: LCA to Start 5 --> 1 --> 3 Direction: "LL" (2 steps) 3 Path: LCA to Dest 5 --> 2 --> 6 Direction: "RL" 4 Combine Paths Replace "LL" with "UU" (go UP from start to LCA) Path Construction: 5 LCA 3 6 U U R L FINAL RESULT Shortest Path from 3 to 6: U U R L Go Up Go Up Right Left Output: "UURL" Step-by-Step Trace: 3 --U--> 1 --U--> 5 LCA | R v 2 --L--> 6 DEST OK - Path Found! Key Insight: The LCA (Lowest Common Ancestor) splits the path into two parts: going UP from start to LCA, then going DOWN from LCA to destination. DFS finds paths from root to both nodes, then we replace the start path directions with 'U's and append the destination path. Time: O(n) TutorialsPoint - Step-By-Step Directions From a Binary Tree Node to Another | DFS with Lowest Common Ancestor
Asked in
Facebook 25 Amazon 18 Google 15 Microsoft 12
89.5K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen