Skip to content

feat: add ts solution to lc problem: No. 2096 #3301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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('');
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down Expand Up @@ -429,4 +483,61 @@ class Solution {

<!-- solution:end -->

<!-- solution:start -->

#### Solution 3: LCA + DFS (Optimized)

<!-- tabs:start -->

#### 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('')
);
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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('');
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down Expand Up @@ -425,4 +479,61 @@ class Solution {

<!-- solution:end -->

<!-- solution:start -->

#### Solution 3: LCA + DFS (Optimized)

<!-- tabs:start -->

#### 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('')
);
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -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('');
};
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
Loading
Loading