Skip to content

Commit fc6e3c8

Browse files
committed
feat: update ts solution to lc problem: No.2096
1 parent b1fe6a2 commit fc6e3c8

File tree

3 files changed

+9
-15
lines changed

3 files changed

+9
-15
lines changed

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

Lines changed: 3 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 => {

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

Lines changed: 3 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 => {

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)