diff --git a/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/README.md b/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/README.md index c351918569317..b76306b4c8808 100644 --- a/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/README.md +++ b/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/README.md @@ -308,15 +308,13 @@ func getDirections(root *TreeNode, startValue int, destValue int) string { function getDirections(root: TreeNode | null, startValue: number, destValue: number): string { const lca = (node: TreeNode | null, p: number, q: number): TreeNode | null => { - if (node === null || node.val === p || node.val === q) { + if (node === null || [p, q].includes(node.val)) { return node; } const left = lca(node.left, p, q); const right = lca(node.right, p, q); - if (left !== null && right !== null) { - return node; - } - return left !== null ? left : right; + + return left && right ? node : left ?? right; }; const dfs = (node: TreeNode | null, x: number, path: string[]): boolean => { @@ -347,6 +345,62 @@ function getDirections(root: TreeNode | null, startValue: number, destValue: num } ``` +#### JavaScript + +```js +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @param {number} startValue + * @param {number} destValue + * @return {string} + */ +var getDirections = function (root, startValue, destValue) { + const lca = (node, p, q) => { + if (node === null || [p, q].includes(node.val)) { + return node; + } + const left = lca(node.left, p, q); + const right = lca(node.right, p, q); + + return left && right ? node : left ?? right; + }; + + const dfs = (node, x, path) => { + if (node === null) { + return false; + } + if (node.val === x) { + return true; + } + path.push('L'); + if (dfs(node.left, x, path)) { + return true; + } + path[path.length - 1] = 'R'; + if (dfs(node.right, x, path)) { + return true; + } + path.pop(); + return false; + }; + + const node = lca(root, startValue, destValue); + const pathToStart = []; + const pathToDest = []; + dfs(node, startValue, pathToStart); + dfs(node, destValue, pathToDest); + return 'U'.repeat(pathToStart.length) + pathToDest.join(''); +}; +``` + @@ -429,4 +483,61 @@ class Solution { + + +#### Solution 3: LCA + DFS (Optimized) + + + +#### TypeScript + +```ts +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ +export function getDirections(root: TreeNode | null, start: number, dest: number): string { + const dfs = (node: TreeNode | null, x: number, path: string[] = []): boolean => { + if (!node) return false; + if (node.val === x) return true; + + path.push('L'); + if (dfs(node.left, x, path)) return true; + + path[path.length - 1] = 'R'; + if (dfs(node.right, x, path)) return true; + path.pop(); + + return false; + }; + + const startPath: string[] = []; + const destPath: string[] = []; + dfs(root, start, startPath); + dfs(root, dest, destPath); + + let i = 0; + while (startPath[i] === destPath[i]) i++; + + return ( + Array(startPath.length - i) + .fill('U') + .join('') + destPath.slice(i).join('') + ); +} +``` + + + + + diff --git a/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/README_EN.md b/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/README_EN.md index 013717c1cd162..94082f2496444 100644 --- a/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/README_EN.md +++ b/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/README_EN.md @@ -304,15 +304,13 @@ func getDirections(root *TreeNode, startValue int, destValue int) string { function getDirections(root: TreeNode | null, startValue: number, destValue: number): string { const lca = (node: TreeNode | null, p: number, q: number): TreeNode | null => { - if (node === null || node.val === p || node.val === q) { + if (node === null || [p, q].includes(node.val)) { return node; } const left = lca(node.left, p, q); const right = lca(node.right, p, q); - if (left !== null && right !== null) { - return node; - } - return left !== null ? left : right; + + return left && right ? node : left ?? right; }; const dfs = (node: TreeNode | null, x: number, path: string[]): boolean => { @@ -343,6 +341,62 @@ function getDirections(root: TreeNode | null, startValue: number, destValue: num } ``` +#### JavaScript + +```js +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @param {number} startValue + * @param {number} destValue + * @return {string} + */ +var getDirections = function (root, startValue, destValue) { + const lca = (node, p, q) => { + if (node === null || [p, q].includes(node.val)) { + return node; + } + const left = lca(node.left, p, q); + const right = lca(node.right, p, q); + + return left && right ? node : left ?? right; + }; + + const dfs = (node, x, path) => { + if (node === null) { + return false; + } + if (node.val === x) { + return true; + } + path.push('L'); + if (dfs(node.left, x, path)) { + return true; + } + path[path.length - 1] = 'R'; + if (dfs(node.right, x, path)) { + return true; + } + path.pop(); + return false; + }; + + const node = lca(root, startValue, destValue); + const pathToStart = []; + const pathToDest = []; + dfs(node, startValue, pathToStart); + dfs(node, destValue, pathToDest); + return 'U'.repeat(pathToStart.length) + pathToDest.join(''); +}; +``` + @@ -425,4 +479,61 @@ class Solution { + + +#### Solution 3: LCA + DFS (Optimized) + + + +#### TypeScript + +```ts +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ +export function getDirections(root: TreeNode | null, start: number, dest: number): string { + const dfs = (node: TreeNode | null, x: number, path: string[] = []): boolean => { + if (!node) return false; + if (node.val === x) return true; + + path.push('L'); + if (dfs(node.left, x, path)) return true; + + path[path.length - 1] = 'R'; + if (dfs(node.right, x, path)) return true; + path.pop(); + + return false; + }; + + const startPath: string[] = []; + const destPath: string[] = []; + dfs(root, start, startPath); + dfs(root, dest, destPath); + + let i = 0; + while (startPath[i] === destPath[i]) i++; + + return ( + Array(startPath.length - i) + .fill('U') + .join('') + destPath.slice(i).join('') + ); +} +``` + + + + + diff --git a/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/Solution.js b/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/Solution.js new file mode 100644 index 0000000000000..c6ca93d60610d --- /dev/null +++ b/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/Solution.js @@ -0,0 +1,51 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @param {number} startValue + * @param {number} destValue + * @return {string} + */ +var getDirections = function (root, startValue, destValue) { + const lca = (node, p, q) => { + if (node === null || [p, q].includes(node.val)) { + return node; + } + const left = lca(node.left, p, q); + const right = lca(node.right, p, q); + + return left && right ? node : left ?? right; + }; + + const dfs = (node, x, path) => { + if (node === null) { + return false; + } + if (node.val === x) { + return true; + } + path.push('L'); + if (dfs(node.left, x, path)) { + return true; + } + path[path.length - 1] = 'R'; + if (dfs(node.right, x, path)) { + return true; + } + path.pop(); + return false; + }; + + const node = lca(root, startValue, destValue); + const pathToStart = []; + const pathToDest = []; + dfs(node, startValue, pathToStart); + dfs(node, destValue, pathToDest); + return 'U'.repeat(pathToStart.length) + pathToDest.join(''); +}; diff --git a/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/Solution.ts b/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/Solution.ts index 4ff0e716117f2..fa2f24e5d72b9 100644 --- a/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/Solution.ts +++ b/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/Solution.ts @@ -14,15 +14,13 @@ function getDirections(root: TreeNode | null, startValue: number, destValue: number): string { const lca = (node: TreeNode | null, p: number, q: number): TreeNode | null => { - if (node === null || node.val === p || node.val === q) { + if (node === null || [p, q].includes(node.val)) { return node; } const left = lca(node.left, p, q); const right = lca(node.right, p, q); - if (left !== null && right !== null) { - return node; - } - return left !== null ? left : right; + + return left && right ? node : left ?? right; }; const dfs = (node: TreeNode | null, x: number, path: string[]): boolean => { diff --git a/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/Solution3.js b/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/Solution3.js new file mode 100644 index 0000000000000..bdf29e35a2190 --- /dev/null +++ b/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/Solution3.js @@ -0,0 +1,43 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @param {number} startValue + * @param {number} destValue + * @return {string} + */ +var getDirections = function (root, start, dest) { + const dfs = (node, x, path = []) => { + if (!node) return false; + if (node.val === x) return true; + + path.push('L'); + if (dfs(node.left, x, path)) return true; + + path[path.length - 1] = 'R'; + if (dfs(node.right, x, path)) return true; + path.pop(); + + return false; + }; + + const startPath = []; + const destPath = []; + dfs(root, start, startPath); + dfs(root, dest, destPath); + + let i = 0; + while (startPath[i] === destPath[i]) i++; + + return ( + Array(startPath.length - i) + .fill('U') + .join('') + destPath.slice(i).join('') + ); +}; diff --git a/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/Solution3.ts b/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/Solution3.ts new file mode 100644 index 0000000000000..6681dd29c13b2 --- /dev/null +++ b/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/Solution3.ts @@ -0,0 +1,42 @@ +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ +export function getDirections(root: TreeNode | null, start: number, dest: number): string { + const dfs = (node: TreeNode | null, x: number, path: string[] = []): boolean => { + if (!node) return false; + if (node.val === x) return true; + + path.push('L'); + if (dfs(node.left, x, path)) return true; + + path[path.length - 1] = 'R'; + if (dfs(node.right, x, path)) return true; + path.pop(); + + return false; + }; + + const startPath: string[] = []; + const destPath: string[] = []; + dfs(root, start, startPath); + dfs(root, dest, destPath); + + let i = 0; + while (startPath[i] === destPath[i]) i++; + + return ( + Array(startPath.length - i) + .fill('U') + .join('') + destPath.slice(i).join('') + ); +}