website/content/ChapterFour/2000~2099/2096.Step-By-Step-Directions-From-a-Binary-Tree-Node-to-Another.md
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.
Example 1:
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:
Input: root = [2,1], startValue = 2, destValue = 1
Output: "L"
Explanation: The shortest path is: 2 → 1.
Constraints:
n.2 <= n <= 1051 <= Node.val <= n1 <= startValue, destValue <= nstartValue != destValue给你一棵 二叉树 的根节点 root ,这棵二叉树总共有 n 个节点。每个节点的值为 1 到 n 中的一个整数,且互不相同。给你一个整数 startValue ,表示起点节点 s 的值,和另一个不同的整数 destValue ,表示终点节点 t 的值。
请找到从节点 s 到节点 t 的 最短路径 ,并以字符串的形式返回每一步的方向。每一步用 大写 字母 'L' ,'R' 和 'U' 分别表示一种方向:
请你返回从 s 到 t 最短路径 每一步的方向。
package leetcode
import (
"github.com/halfrost/leetcode-go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func getDirections(root *TreeNode, startValue int, destValue int) string {
sPath, dPath := make([]byte, 0), make([]byte, 0)
findPath(root, startValue, &sPath)
findPath(root, destValue, &dPath)
size, i := min(len(sPath), len(dPath)), 0
for i < size {
if sPath[len(sPath)-1-i] == dPath[len(dPath)-1-i] {
i++
} else {
break
}
}
sPath = sPath[:len(sPath)-i]
replace(sPath)
dPath = dPath[:len(dPath)-i]
reverse(dPath)
sPath = append(sPath, dPath...)
return string(sPath)
}
func findPath(root *TreeNode, value int, path *[]byte) bool {
if root.Val == value {
return true
}
if root.Left != nil && findPath(root.Left, value, path) {
*path = append(*path, 'L')
return true
}
if root.Right != nil && findPath(root.Right, value, path) {
*path = append(*path, 'R')
return true
}
return false
}
func reverse(path []byte) {
left, right := 0, len(path)-1
for left < right {
path[left], path[right] = path[right], path[left]
left++
right--
}
}
func replace(path []byte) {
for i := 0; i < len(path); i++ {
path[i] = 'U'
}
}
func min(i, j int) int {
if i < j {
return i
}
return j
}