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

Given the root of a binary tree with n uniquely-valued nodes and two values, startValue and destValue, representing the values of the start and destination nodes respectively. Find the shortest path from the start node to the destination node and return the step-by-step directions as a string consisting of 'L', 'R', and 'U' indicating left, right, and up moves respectively.

Constraints

  • The number of nodes in the tree is n.
  • 2 <= n <= 10^5
  • 1 <= Node.val <= n
  • All the values in the tree are unique.
  • 1 <= startValue, destValue <= n
  • startValue != destValue
Saving… · 10 lines
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right

class Solution:
def stepByStepDirectionsFromABinaryTreeNodeToAnother(self, root, startValue, destValue) -> str:
Console

Click Run to execute sample tests.