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

<p>Find the <strong>shortest path</strong> starting from node <code>s</code> and ending at node <code>t</code>. Generate step-by-step directions of such path as a string consisting of only the <strong>uppercase</strong> letters <code>&#39;L&#39;</code>, <code>&#39;R&#39;</code>, and <code>&#39;U&#39;</code>. Each letter indicates a specific direction:</p>

<ul>
	<li><code>&#39;L&#39;</code> means to go from a node to its <strong>left child</strong> node.</li>
	<li><code>&#39;R&#39;</code> means to go from a node to its <strong>right child</strong> node.</li>
	<li><code>&#39;U&#39;</code> means to go from a node to its <strong>parent</strong> node.</li>
</ul>

<p>Return <em>the step-by-step directions of the <strong>shortest path</strong> from node </em><code>s</code><em> to node</em> <code>t</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/15/eg1.png" style="width: 214px; height: 163px;" />
<pre>
<strong>Input:</strong> root = [5,1,2,3,null,6,4], startValue = 3, destValue = 6
<strong>Output:</strong> &quot;UURL&quot;
<strong>Explanation:</strong> The shortest path is: 3 &rarr; 1 &rarr; 5 &rarr; 2 &rarr; 6.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/15/eg2.png" style="width: 74px; height: 102px;" />
<pre>
<strong>Input:</strong> root = [2,1], startValue = 2, destValue = 1
<strong>Output:</strong> &quot;L&quot;
<strong>Explanation:</strong> The shortest path is: 2 &rarr; 1.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is <code>n</code>.</li>
	<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= Node.val &lt;= n</code></li>
	<li>All the values in the tree are <strong>unique</strong>.</li>
	<li><code>1 &lt;= startValue, destValue &lt;= n</code></li>
	<li><code>startValue != destValue</code></li>
</ul>

# 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
"""
Approach: FindLCA + FindDirections
    1. Find LCA (Lowest Common Ancestor)
        1. create a helper function find_lca_helper(x):
            1. this function returns a namedtuple LCAOutput with two attributes:
                1. num_target_node (int): number of target nodes found in a subtree x
                2. lca (int): the lca found in this subtree
            2. traverse the left subtree using find_lca_help(x.left)
                1. if left_subtree.num_target_node == 2:
                    return left_subtree_lca
            3. traverse the right subtree using find_lca_help(x.right)
                2. if right_subtree.num_target_node == 2:
                    return right_subtree_lca
            4. otherwise:
                num_target = left_subtree.num_target_node + right_subtree.num_target_node + x.val == startValue + x.val == destValue
                lca = x
    2. With the LCA, we traverse downward to find the directions:
        1. find_directions(x, depth, directions):
            if x.val == startValue:
                we know that we need `depth` * "U"
            if x.val == destValue:
                we need to copy the directions
            
            1. traverse left subtree:
                1. add "L" into directions
                2. call find_directions(x, depth +1, directions)
                3. pop out "L" from directions
            
            2. traverse right subtree:
                similar to the above but use "R"

TC: O(N), N denotes number of nodes
    Find_LCA - O(N)
    find_directions - O(N)
SC: O(n)
    DFS search at most requires O(n) recursive stack
"""
from collections import namedtuple
class Solution:
    def getDirections(self, root: Optional[TreeNode], startValue: int, destValue: int) -> str:
        if root is None:
            raise ValueError('The root node should not be a Null node')
        
        LCAOutput = namedtuple('LCAOutput', ['num_target', 'lca'])
        
        def find_LCA(x):
            if x is None:
                return LCAOutput(0, x)
            
            left_subtree = find_LCA(x.left)
            if left_subtree.num_target == 2:
                return left_subtree

            right_subtree = find_LCA(x.right)
            if right_subtree.num_target == 2:
                return right_subtree
            
            num_target = (left_subtree.num_target + right_subtree.num_target 
                            + int(x.val == startValue) + int(x.val == destValue))
            return LCAOutput(num_target, x)
        
        def find_directions(x, depth, directions):
            if not x:
                return
            
            if x.val == startValue:
                self.num_steps_to_start = depth
            if x.val == destValue:
                self.directions = directions.copy()
            
            directions.append('L')
            find_directions(x.left, depth + 1, directions)
            directions.pop()

            directions.append('R')
            find_directions(x.right, depth + 1, directions)
            directions.pop()



        lca_output = find_LCA(root)
        self.num_steps_to_start = 0
        self.directions = []
        find_directions(lca_output.lca, 0, [])

        return self.num_steps_to_start * "U" + "".join(self.directions)

        
516