Skip to content

Commit 1fb115a

Browse files
committed
feat: add js solution to lc problem: No. 2096
1 parent 24027fa commit 1fb115a

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,61 @@ class Solution {
483483
484484
<!-- solution:end -->
485485
486+
<!-- solution:start -->
487+
488+
#### Solution 3: LCA + DFS (Optimized)
489+
490+
<!-- tabs:start -->
491+
492+
#### TypeScript
493+
494+
```ts
495+
/**
496+
* Definition for a binary tree node.
497+
* class TreeNode {
498+
* val: number
499+
* left: TreeNode | null
500+
* right: TreeNode | null
501+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
502+
* this.val = (val===undefined ? 0 : val)
503+
* this.left = (left===undefined ? null : left)
504+
* this.right = (right===undefined ? null : right)
505+
* }
506+
* }
507+
*/
508+
export function getDirections(root: TreeNode | null, start: number, dest: number): string {
509+
const dfs = (node: TreeNode | null, x: number, path: string[] = []): boolean => {
510+
if (!node) return false;
511+
if (node.val === x) return true;
512+
513+
path.push('L');
514+
if (dfs(node.left, x, path)) return true;
515+
516+
path[path.length - 1] = 'R';
517+
if (dfs(node.right, x, path)) return true;
518+
path.pop();
519+
520+
return false;
521+
};
522+
523+
const startPath: string[] = [];
524+
const destPath: string[] = [];
525+
dfs(root, start, startPath);
526+
dfs(root, dest, destPath);
527+
528+
let i = 0;
529+
while (startPath[i] === destPath[i]) i++;
530+
531+
return (
532+
Array(startPath.length - i)
533+
.fill('U')
534+
.join('') + destPath.slice(i).join('')
535+
);
536+
}
537+
```
538+
539+
<!-- tabs:end -->
540+
541+
<!-- solution:end -->
542+
486543
<!-- problem:end -->

solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/README_EN.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,4 +479,61 @@ class Solution {
479479
480480
<!-- solution:end -->
481481
482+
<!-- solution:start -->
483+
484+
#### Solution 3: LCA + DFS (Optimized)
485+
486+
<!-- tabs:start -->
487+
488+
#### TypeScript
489+
490+
```ts
491+
/**
492+
* Definition for a binary tree node.
493+
* class TreeNode {
494+
* val: number
495+
* left: TreeNode | null
496+
* right: TreeNode | null
497+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
498+
* this.val = (val===undefined ? 0 : val)
499+
* this.left = (left===undefined ? null : left)
500+
* this.right = (right===undefined ? null : right)
501+
* }
502+
* }
503+
*/
504+
export function getDirections(root: TreeNode | null, start: number, dest: number): string {
505+
const dfs = (node: TreeNode | null, x: number, path: string[] = []): boolean => {
506+
if (!node) return false;
507+
if (node.val === x) return true;
508+
509+
path.push('L');
510+
if (dfs(node.left, x, path)) return true;
511+
512+
path[path.length - 1] = 'R';
513+
if (dfs(node.right, x, path)) return true;
514+
path.pop();
515+
516+
return false;
517+
};
518+
519+
const startPath: string[] = [];
520+
const destPath: string[] = [];
521+
dfs(root, start, startPath);
522+
dfs(root, dest, destPath);
523+
524+
let i = 0;
525+
while (startPath[i] === destPath[i]) i++;
526+
527+
return (
528+
Array(startPath.length - i)
529+
.fill('U')
530+
.join('') + destPath.slice(i).join('')
531+
);
532+
}
533+
```
534+
535+
<!-- tabs:end -->
536+
537+
<!-- solution:end -->
538+
482539
<!-- problem:end -->
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @param {number} startValue
12+
* @param {number} destValue
13+
* @return {string}
14+
*/
15+
var getDirections = function (root, start, dest) {
16+
const dfs = (node, x, path = []) => {
17+
if (!node) return false;
18+
if (node.val === x) return true;
19+
20+
path.push('L');
21+
if (dfs(node.left, x, path)) return true;
22+
23+
path[path.length - 1] = 'R';
24+
if (dfs(node.right, x, path)) return true;
25+
path.pop();
26+
27+
return false;
28+
};
29+
30+
const startPath = [];
31+
const destPath = [];
32+
dfs(root, start, startPath);
33+
dfs(root, dest, destPath);
34+
35+
let i = 0;
36+
while (startPath[i] === destPath[i]) i++;
37+
38+
return (
39+
Array(startPath.length - i)
40+
.fill('U')
41+
.join('') + destPath.slice(i).join('')
42+
);
43+
};

0 commit comments

Comments
 (0)