Skip to content

Commit f583f51

Browse files
committed
feat: add js solution to lc problem: No. 2096
1 parent fc6e3c8 commit f583f51

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,62 @@ function getDirections(root: TreeNode | null, startValue: number, destValue: num
345345
}
346346
```
347347

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+
348404
<!-- tabs:end -->
349405
350406
<!-- solution:end -->

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,62 @@ function getDirections(root: TreeNode | null, startValue: number, destValue: num
341341
}
342342
```
343343

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+
344400
<!-- tabs:end -->
345401
346402
<!-- solution: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+
};

0 commit comments

Comments
 (0)