Skip to content

feat: add typescript solution to lc problem: No.0987 #625

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 1 commit into from
Dec 9, 2021
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 @@ -83,6 +83,61 @@

```

### **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)
* }
* }
*/

function verticalTraversal(root: TreeNode | null): number[][] {
let solution = [];
dfs(root, 0, 0, solution);
// 优先依据i=2排序, 然后依据i=1排序
solution.sort(compare);
// console.log(solution);
let ans = [];
let pre = Number.MIN_SAFE_INTEGER;
for (let node of solution) {
const [val, , idx] = node;
if (idx != pre) {
ans.push([]);
pre = idx;
}
ans[ans.length - 1].push(val);
}
return ans;
};

function compare(a: Array<number>, b: Array<number>) {
const [a0, a1, a2] = a, [b0, b1, b2] = b;
if (a2 == b2) {
if (a1 == b1) {
return a0 - b0;
}
return a1 - b1;
}
return a2 - b2;
}

function dfs(root: TreeNode | null, depth: number, idx: number, solution: Array<Array<number>>) {
if (!root) return;
solution.push([root.val, depth, idx]);
dfs(root.left, depth + 1, idx - 1, solution);
dfs(root.right, depth + 1, idx + 1, solution);
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,61 @@ Note that the solution remains the same since 5 and 6 are in the same location a

```

### **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)
* }
* }
*/

function verticalTraversal(root: TreeNode | null): number[][] {
let solution = [];
dfs(root, 0, 0, solution);
// 优先依据i=2排序, 然后依据i=1排序
solution.sort(compare);
// console.log(solution);
let ans = [];
let pre = Number.MIN_SAFE_INTEGER;
for (let node of solution) {
const [val, , idx] = node;
if (idx != pre) {
ans.push([]);
pre = idx;
}
ans[ans.length - 1].push(val);
}
return ans;
};

function compare(a: Array<number>, b: Array<number>) {
const [a0, a1, a2] = a, [b0, b1, b2] = b;
if (a2 == b2) {
if (a1 == b1) {
return a0 - b0;
}
return a1 - b1;
}
return a2 - b2;
}

function dfs(root: TreeNode | null, depth: number, idx: number, solution: Array<Array<number>>) {
if (!root) return;
solution.push([root.val, depth, idx]);
dfs(root.left, depth + 1, idx - 1, solution);
dfs(root.right, depth + 1, idx + 1, solution);
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* 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)
* }
* }
*/

function verticalTraversal(root: TreeNode | null): number[][] {
let solution = [];
dfs(root, 0, 0, solution);
// 优先依据i=2排序, 然后依据i=1排序
solution.sort(compare);
// console.log(solution);
let ans = [];
let pre = Number.MIN_SAFE_INTEGER;
for (let node of solution) {
const [val, , idx] = node;
if (idx != pre) {
ans.push([]);
pre = idx;
}
ans[ans.length - 1].push(val);
}
return ans;
};

function compare(a: Array<number>, b: Array<number>) {
const [a0, a1, a2] = a, [b0, b1, b2] = b;
if (a2 == b2) {
if (a1 == b1) {
return a0 - b0;
}
return a1 - b1;
}
return a2 - b2;
}

function dfs(root: TreeNode | null, depth: number, idx: number, solution: Array<Array<number>>) {
if (!root) return;
solution.push([root.val, depth, idx]);
dfs(root.left, depth + 1, idx - 1, solution);
dfs(root.right, depth + 1, idx + 1, solution);
}