> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Step-By-Step Directions From a Binary Tree

> Tested Python solution for LeetCode 2096 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 2096, [Medium](/catalog/medium). Topics: [String](/catalog/topics/string), [Tree](/catalog/topics/tree), [Depth-First Search](/catalog/topics/depth-first-search), [Binary Tree](/catalog/topics/binary-tree), Binary Lifting, Lowest Common Ancestor. [View on LeetCode](https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2096   # by problem number
lcpy gen -s step_by_step_directions_from_a_binary_tree_node_to_another   # by problem name
```

## 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`.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/11/15/eg1.png)

```
Input: root = [5,1,2,3,null,6,4], startValue = 3, destValue = 6
Output: "UURL"
Explanation: The shortest path is: 3 -> 1 -> 5 -> 2 -> 6.
```

![Example 2](https://assets.leetcode.com/uploads/2021/11/15/eg2.png)

```
Input: root = [2,1], startValue = 2, destValue = 1
Output: "L"
Explanation: The shortest path is: 2 -> 1.
```

### 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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/step_by_step_directions_from_a_binary_tree_node_to_another/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/step_by_step_directions_from_a_binary_tree_node_to_another/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import TreeNode


class Solution:
    # Time: O(n)
    # Space: O(h)
    def get_directions(self, root: TreeNode[int] | None, start_value: int, dest_value: int) -> str:
        def find(node: TreeNode[int] | None, target: int, path: list[str]) -> list[str] | None:
            if node is None:
                return None
            if node.val == target:
                return list(path)
            path.append("L")
            found = find(node.left, target, path)
            if found is not None:
                return found
            path[-1] = "R"
            found = find(node.right, target, path)
            if found is not None:
                return found
            path.pop()
            return None

        if root is None:
            return ""

        start_path = find(root, start_value, [])
        dest_path = find(root, dest_value, [])
        if start_path is None or dest_path is None:
            return ""

        shared = 0
        while (
            shared < len(start_path)
            and shared < len(dest_path)
            and start_path[shared] == dest_path[shared]
        ):
            shared += 1

        return "U" * (len(start_path) - shared) + "".join(dest_path[shared:])
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(h) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.