← 返回 snowflake 的题目列表Step-By-Step Directions From a Binary Tree Node to Another
类型:qbank
You are given the root of a binary tree with n nodes where each node has a unique value from 1 to n.
Finding the Shortest Path Between Two Nodes in a Binary Tree
Problem Overview
You are provided with the root of a binary tree that contains n nodes. Every node in this tree has a unique value between 1 and n. You are also given two specific numbers: startValue and destValue.
Your task is to find the shortest path to travel from the node labeled startValue to the node labeled destValue. You must return this path as a string.
Use the following letters to represent the movements:
'L': Move down to the left child node.
'R': Move down to the right child node.
'U': Move up to the parent node.
Sample Cases
Case 1:
Input: root = [5,1,2,3,null,6,4], startValue = 3, destValue = 6
Output: "UURL"
Case 2:
Input: root = [2,1], startValue = 2, destValue = 1
Output: "L"
Input Limits
The number of nodes (n) is between 2 and 100,000 (10^5).
Each Node.val is between 1 and n.
All values in the tree are unique (no duplicates).
The startValue is never the same as the destValue.
Both startValue and destValue are guaranteed to be inside the tree.