Skip to content

Commit 8792fb2

Browse files
authored
feat: add ts solution to lc problem: No. 2096 (#3301)
1 parent b1fe6a2 commit 8792fb2

File tree

6 files changed

+371
-15
lines changed

6 files changed

+371
-15
lines changed

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

Lines changed: 116 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,15 +308,13 @@ func getDirections(root *TreeNode, startValue int, destValue int) string {
308308

309309
function getDirections(root: TreeNode | null, startValue: number, destValue: number): string {
310310
const lca = (node: TreeNode | null, p: number, q: number): TreeNode | null => {
311-
if (node === null || node.val === p || node.val === q) {
311+
if (node === null || [p, q].includes(node.val)) {
312312
return node;
313313
}
314314
const left = lca(node.left, p, q);
315315
const right = lca(node.right, p, q);
316-
if (left !== null && right !== null) {
317-
return node;
318-
}
319-
return left !== null ? left : right;
316+
317+
return left && right ? node : left ?? right;
320318
};
321319

322320
const dfs = (node: TreeNode | null, x: number, path: string[]): boolean => {
@@ -347,6 +345,62 @@ function getDirections(root: TreeNode | null, startValue: number, destValue: num
347345
}
348346
```
349347

348+
#### JavaScript
349+
350+
```js
351+
/**
352+
* Definition for a binary tree node.
353+
* function TreeNode(val, left, right) {
354+
* this.val = (val===undefined ? 0 : val)
355+
* this.left = (left===undefined ? null : left)
356+
* this.right = (right===undefined ? null : right)
357+
* }
358+
*/
359+
/**
360+
* @param {TreeNode} root
361+
* @param {number} startValue
362+
* @param {number} destValue
363+
* @return {string}
364+
*/
365+
var getDirections = function (root, startValue, destValue) {
366+
const lca = (node, p, q) => {
367+
if (node === null || [p, q].includes(node.val)) {
368+
return node;
369+
}
370+
const left = lca(node.left, p, q);
371+
const right = lca(node.right, p, q);
372+
373+
return left && right ? node : left ?? right;
374+
};
375+
376+
const dfs = (node, x, path) => {
377+
if (node === null) {
378+
return false;
379+
}
380+
if (node.val === x) {
381+
return true;
382+
}
383+
path.push('L');
384+
if (dfs(node.left, x, path)) {
385+
return true;
386+
}
387+
path[path.length - 1] = 'R';
388+
if (dfs(node.right, x, path)) {
389+
return true;
390+
}
391+
path.pop();
392+
return false;
393+
};
394+
395+
const node = lca(root, startValue, destValue);
396+
const pathToStart = [];
397+
const pathToDest = [];
398+
dfs(node, startValue, pathToStart);
399+
dfs(node, destValue, pathToDest);
400+
return 'U'.repeat(pathToStart.length) + pathToDest.join('');
401+
};
402+
```
403+
350404
<!-- tabs:end -->
351405
352406
<!-- solution:end -->
@@ -429,4 +483,61 @@ class Solution {
429483
430484
<!-- solution:end -->
431485
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+
432543
<!-- problem:end -->

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

Lines changed: 116 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,15 +304,13 @@ func getDirections(root *TreeNode, startValue int, destValue int) string {
304304

305305
function getDirections(root: TreeNode | null, startValue: number, destValue: number): string {
306306
const lca = (node: TreeNode | null, p: number, q: number): TreeNode | null => {
307-
if (node === null || node.val === p || node.val === q) {
307+
if (node === null || [p, q].includes(node.val)) {
308308
return node;
309309
}
310310
const left = lca(node.left, p, q);
311311
const right = lca(node.right, p, q);
312-
if (left !== null && right !== null) {
313-
return node;
314-
}
315-
return left !== null ? left : right;
312+
313+
return left && right ? node : left ?? right;
316314
};
317315

318316
const dfs = (node: TreeNode | null, x: number, path: string[]): boolean => {
@@ -343,6 +341,62 @@ function getDirections(root: TreeNode | null, startValue: number, destValue: num
343341
}
344342
```
345343

344+
#### JavaScript
345+
346+
```js
347+
/**
348+
* Definition for a binary tree node.
349+
* function TreeNode(val, left, right) {
350+
* this.val = (val===undefined ? 0 : val)
351+
* this.left = (left===undefined ? null : left)
352+
* this.right = (right===undefined ? null : right)
353+
* }
354+
*/
355+
/**
356+
* @param {TreeNode} root
357+
* @param {number} startValue
358+
* @param {number} destValue
359+
* @return {string}
360+
*/
361+
var getDirections = function (root, startValue, destValue) {
362+
const lca = (node, p, q) => {
363+
if (node === null || [p, q].includes(node.val)) {
364+
return node;
365+
}
366+
const left = lca(node.left, p, q);
367+
const right = lca(node.right, p, q);
368+
369+
return left && right ? node : left ?? right;
370+
};
371+
372+
const dfs = (node, x, path) => {
373+
if (node === null) {
374+
return false;
375+
}
376+
if (node.val === x) {
377+
return true;
378+
}
379+
path.push('L');
380+
if (dfs(node.left, x, path)) {
381+
return true;
382+
}
383+
path[path.length - 1] = 'R';
384+
if (dfs(node.right, x, path)) {
385+
return true;
386+
}
387+
path.pop();
388+
return false;
389+
};
390+
391+
const node = lca(root, startValue, destValue);
392+
const pathToStart = [];
393+
const pathToDest = [];
394+
dfs(node, startValue, pathToStart);
395+
dfs(node, destValue, pathToDest);
396+
return 'U'.repeat(pathToStart.length) + pathToDest.join('');
397+
};
398+
```
399+
346400
<!-- tabs:end -->
347401
348402
<!-- solution:end -->
@@ -425,4 +479,61 @@ class Solution {
425479
426480
<!-- solution:end -->
427481
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+
428539
<!-- problem:end -->
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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, startValue, destValue) {
16+
const lca = (node, p, q) => {
17+
if (node === null || [p, q].includes(node.val)) {
18+
return node;
19+
}
20+
const left = lca(node.left, p, q);
21+
const right = lca(node.right, p, q);
22+
23+
return left && right ? node : left ?? right;
24+
};
25+
26+
const dfs = (node, x, path) => {
27+
if (node === null) {
28+
return false;
29+
}
30+
if (node.val === x) {
31+
return true;
32+
}
33+
path.push('L');
34+
if (dfs(node.left, x, path)) {
35+
return true;
36+
}
37+
path[path.length - 1] = 'R';
38+
if (dfs(node.right, x, path)) {
39+
return true;
40+
}
41+
path.pop();
42+
return false;
43+
};
44+
45+
const node = lca(root, startValue, destValue);
46+
const pathToStart = [];
47+
const pathToDest = [];
48+
dfs(node, startValue, pathToStart);
49+
dfs(node, destValue, pathToDest);
50+
return 'U'.repeat(pathToStart.length) + pathToDest.join('');
51+
};

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@
1414

1515
function getDirections(root: TreeNode | null, startValue: number, destValue: number): string {
1616
const lca = (node: TreeNode | null, p: number, q: number): TreeNode | null => {
17-
if (node === null || node.val === p || node.val === q) {
17+
if (node === null || [p, q].includes(node.val)) {
1818
return node;
1919
}
2020
const left = lca(node.left, p, q);
2121
const right = lca(node.right, p, q);
22-
if (left !== null && right !== null) {
23-
return node;
24-
}
25-
return left !== null ? left : right;
22+
23+
return left && right ? node : left ?? right;
2624
};
2725

2826
const dfs = (node: TreeNode | null, x: number, path: string[]): boolean => {

0 commit comments

Comments
 (0)