From 688e52fdc26e0318677fc0ff2692ffac4456b754 Mon Sep 17 00:00:00 2001 From: zhaocchen Date: Thu, 9 Dec 2021 16:12:29 +0800 Subject: [PATCH] feat: add typescript solution to lc problem: No.0987 No.0987.Vertical Order Traversal of a Binary Tree --- .../README.md | 55 +++++++++++++++++++ .../README_EN.md | 55 +++++++++++++++++++ .../Solution.ts | 50 +++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/Solution.ts diff --git a/solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/README.md b/solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/README.md index e1edffebfa692..d49d86f1f2f00 100644 --- a/solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/README.md +++ b/solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/README.md @@ -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, b: Array) { + 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>) { + 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); +} +``` + ### **...** ``` diff --git a/solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/README_EN.md b/solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/README_EN.md index d128f19c5a9cb..e61380d149da9 100644 --- a/solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/README_EN.md +++ b/solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/README_EN.md @@ -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, b: Array) { + 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>) { + 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); +} +``` + ### **...** ``` diff --git a/solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/Solution.ts b/solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/Solution.ts new file mode 100644 index 0000000000000..e4c464405518f --- /dev/null +++ b/solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/Solution.ts @@ -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, b: Array) { + 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>) { + 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); +} \ No newline at end of file