|
| 1 | +# 2096. Step-By-Step Directions From a Binary Tree Node to Another |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: String, Tree, Depth-First Search, Binary Tree. |
| 5 | +- Similar Questions: Path Sum II, Lowest Common Ancestor of a Binary Tree, Binary Tree Paths, Find Distance in a Binary Tree. |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +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`. |
| 10 | + |
| 11 | +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: |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +- `'L'` means to go from a node to its **left child** node. |
| 16 | + |
| 17 | +- `'R'` means to go from a node to its **right child** node. |
| 18 | + |
| 19 | +- `'U'` means to go from a node to its **parent** node. |
| 20 | + |
| 21 | + |
| 22 | +Return **the step-by-step directions of the **shortest path** from node **`s`** to node** `t`. |
| 23 | + |
| 24 | + |
| 25 | +Example 1: |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +``` |
| 30 | +Input: root = [5,1,2,3,null,6,4], startValue = 3, destValue = 6 |
| 31 | +Output: "UURL" |
| 32 | +Explanation: The shortest path is: 3 → 1 → 5 → 2 → 6. |
| 33 | +``` |
| 34 | + |
| 35 | +Example 2: |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +``` |
| 40 | +Input: root = [2,1], startValue = 2, destValue = 1 |
| 41 | +Output: "L" |
| 42 | +Explanation: The shortest path is: 2 → 1. |
| 43 | +``` |
| 44 | + |
| 45 | + |
| 46 | +**Constraints:** |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +- The number of nodes in the tree is `n`. |
| 51 | + |
| 52 | +- `2 <= n <= 105` |
| 53 | + |
| 54 | +- `1 <= Node.val <= n` |
| 55 | + |
| 56 | +- All the values in the tree are **unique**. |
| 57 | + |
| 58 | +- `1 <= startValue, destValue <= n` |
| 59 | + |
| 60 | +- `startValue != destValue` |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +## Solution |
| 65 | + |
| 66 | +```javascript |
| 67 | +/** |
| 68 | + * Definition for a binary tree node. |
| 69 | + * function TreeNode(val, left, right) { |
| 70 | + * this.val = (val===undefined ? 0 : val) |
| 71 | + * this.left = (left===undefined ? null : left) |
| 72 | + * this.right = (right===undefined ? null : right) |
| 73 | + * } |
| 74 | + */ |
| 75 | +/** |
| 76 | + * @param {TreeNode} root |
| 77 | + * @param {number} startValue |
| 78 | + * @param {number} destValue |
| 79 | + * @return {string} |
| 80 | + */ |
| 81 | +var getDirections = function(root, startValue, destValue) { |
| 82 | + var findLCA = function(node) { |
| 83 | + if (!node) return false; |
| 84 | + if (node.val === startValue || node.val === destValue) { |
| 85 | + return node; |
| 86 | + } |
| 87 | + var left = findLCA(node.left); |
| 88 | + var right = findLCA(node.right); |
| 89 | + return (left && right) ? node : (left || right || false); |
| 90 | + }; |
| 91 | + var findStart = function(node) { |
| 92 | + if (!node) return null; |
| 93 | + if (node.val === startValue) return ''; |
| 94 | + |
| 95 | + var left = findStart(node.left); |
| 96 | + if (left !== null) return left + 'U'; |
| 97 | + |
| 98 | + var right = findStart(node.right); |
| 99 | + if (right !== null) return right + 'U'; |
| 100 | + |
| 101 | + return null; |
| 102 | + }; |
| 103 | + var findDest = function(node) { |
| 104 | + if (!node) return null; |
| 105 | + if (node.val === destValue) return ''; |
| 106 | + |
| 107 | + var left = findDest(node.left); |
| 108 | + if (left !== null) return 'L' + left; |
| 109 | + |
| 110 | + var right = findDest(node.right); |
| 111 | + if (right !== null) return 'R' + right; |
| 112 | + |
| 113 | + return null; |
| 114 | + }; |
| 115 | + var LCA = findLCA(root); |
| 116 | + var startPath = findStart(LCA); |
| 117 | + var destPath = findDest(LCA); |
| 118 | + return startPath + destPath; |
| 119 | +}; |
| 120 | +``` |
| 121 | + |
| 122 | +**Explain:** |
| 123 | + |
| 124 | +nope. |
| 125 | + |
| 126 | +**Complexity:** |
| 127 | + |
| 128 | +* Time complexity : O(n). |
| 129 | +* Space complexity : O(n). |
0 commit comments