diff --git a/.github/workflows/code-quality.yml b/.github/backups/code-quality.yml similarity index 100% rename from .github/workflows/code-quality.yml rename to .github/backups/code-quality.yml diff --git a/.github/workflows/openai-codex-pr-review.yml b/.github/backups/openai-codex-pr-review.yml similarity index 100% rename from .github/workflows/openai-codex-pr-review.yml rename to .github/backups/openai-codex-pr-review.yml diff --git a/1029-vertical-order-traversal-of-a-binary-tree/README.md b/1029-vertical-order-traversal-of-a-binary-tree/README.md deleted file mode 100644 index eb994a9..0000000 --- a/1029-vertical-order-traversal-of-a-binary-tree/README.md +++ /dev/null @@ -1,57 +0,0 @@ - -# [Vertical Order Traversal of a Binary Tree](https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree) ![](https://img.shields.io/badge/Hard-red) - -

Given the root of a binary tree, calculate the vertical order traversal of the binary tree.

- -

For each node at position (row, col), its left and right children will be at positions (row + 1, col - 1) and (row + 1, col + 1) respectively. The root of the tree is at (0, 0).

- -

The vertical order traversal of a binary tree is a list of top-to-bottom orderings for each column index starting from the leftmost column and ending on the rightmost column. There may be multiple nodes in the same row and same column. In such a case, sort these nodes by their values.

- -

Return the vertical order traversal of the binary tree.

- -

 

-

Example 1:

- -
-Input: root = [3,9,20,null,null,15,7]
-Output: [[9],[3,15],[20],[7]]
-Explanation:
-Column -1: Only node 9 is in this column.
-Column 0: Nodes 3 and 15 are in this column in that order from top to bottom.
-Column 1: Only node 20 is in this column.
-Column 2: Only node 7 is in this column.
- -

Example 2:

- -
-Input: root = [1,2,3,4,5,6,7]
-Output: [[4],[2],[1,5,6],[3],[7]]
-Explanation:
-Column -2: Only node 4 is in this column.
-Column -1: Only node 2 is in this column.
-Column 0: Nodes 1, 5, and 6 are in this column.
-          1 is at the top, so it comes first.
-          5 and 6 are at the same position (2, 0), so we order them by their value, 5 before 6.
-Column 1: Only node 3 is in this column.
-Column 2: Only node 7 is in this column.
-
- -

Example 3:

- -
-Input: root = [1,2,3,4,6,5,7]
-Output: [[4],[2],[1,5,6],[3],[7]]
-Explanation:
-This case is the exact same as example 2, but with nodes 5 and 6 swapped.
-Note that the solution remains the same since 5 and 6 are in the same location and should be ordered by their values.
-
- -

 

-

Constraints:

- - - - \ No newline at end of file diff --git a/README.md b/README.md index d25812a..31be0e7 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ A comprehensive collection of coding challenges from multiple platforms for lear | Platform | Focus Area | Problems Solved | | ------------------------------- | ---------------------------- | --------------- | -| [LeetCode](#leetcode) | Data Structures & Algorithms | 171 | +| [LeetCode](#leetcode) | Data Structures & Algorithms | 172 | | [GreatFrontEnd](#greatfrontend) | Frontend Engineering | 15 | ## Platforms @@ -287,7 +287,7 @@ Various places where I enjoy practicing solving coding challenges:
-Hard Problems (14 solved) +Hard Problems (15 solved) ### Array & Dynamic Programming @@ -295,6 +295,10 @@ Various places where I enjoy practicing solving coding challenges: - [0689 - Maximum Sum of 3 Non-Overlapping Subarrays](./leetcode/hard/0689-maximum-sum-of-3-non-overlapping-subarrays) ![Hard](https://img.shields.io/badge/Hard-red) - [1034 - Subarrays with K Different Integers](./leetcode/hard/1034-subarrays-with-k-different-integers) ![Hard](https://img.shields.io/badge/Hard-red) +### Tree Problems + +- [0987 - Vertical Order Traversal of a Binary Tree](./leetcode/hard/0987-vertical-order-traversal-of-a-binary-tree) ![Hard](https://img.shields.io/badge/Hard-red) + ### Backtracking & Trie - [0212 - Word Search II](./leetcode/hard/0212-word-search-ii) ![Hard](https://img.shields.io/badge/Hard-red) diff --git a/leetcode/hard/0987-vertical-order-traversal-of-a-binary-tree/README.md b/leetcode/hard/0987-vertical-order-traversal-of-a-binary-tree/README.md new file mode 100644 index 0000000..f5c9b03 --- /dev/null +++ b/leetcode/hard/0987-vertical-order-traversal-of-a-binary-tree/README.md @@ -0,0 +1,57 @@ +# [Vertical Order Traversal of a Binary Tree](https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree) ![Shield](https://img.shields.io/badge/Hard-red) + +Given the `root` of a binary tree, calculate the **vertical order traversal** of the binary tree. + +For each node at position `(row, col)`, its left and right children will be at positions `(row + 1, col - 1)` and `(row + 1, col + 1)` respectively. The root of the tree is at `(0, 0)`. + +The **vertical order traversal** of a binary tree is a list of top-to-bottom orderings for each column index starting from the leftmost column and ending on the rightmost column. There may be multiple nodes in the same row and same column. In such a case, sort these nodes by their values. + +Return _the **vertical order traversal** of the binary tree_. + +## Example 1 + +![tree](https://assets.leetcode.com/uploads/2021/01/29/vtree1.jpg) + +```bash +Input: root = [3,9,20,null,null,15,7] +Output: [[9],[3,15],[20],[7]] +Explanation: +Column -1: Only node 9 is in this column. +Column 0: Nodes 3 and 15 are in this column in that order from top to bottom. +Column 1: Only node 20 is in this column. +Column 2: Only node 7 is in this column. +``` + +## Example 2 + +![tree](https://assets.leetcode.com/uploads/2021/01/29/vtree2.jpg) + +```bash +Input: root = [1,2,3,4,5,6,7] +Output: [[4],[2],[1,5,6],[3],[7]] +Explanation: +Column -2: Only node 4 is in this column. +Column -1: Only node 2 is in this column. +Column 0: Nodes 1, 5, and 6 are in this column. + 1 is at the top, so it comes first. + 5 and 6 are at the same position (2, 0), so we order them by their value, 5 before 6. +Column 1: Only node 3 is in this column. +Column 2: Only node 7 is in this column. +``` + +## Example 3 + +![tree](https://assets.leetcode.com/uploads/2021/01/29/vtree3.jpg) + +```bash +Input: root = [1,2,3,4,6,5,7] +Output: [[4],[2],[1,5,6],[3],[7]] +Explanation: +This case is the exact same as example 2, but with nodes 5 and 6 swapped. +Note that the solution remains the same since 5 and 6 are in the same location and should be ordered by their values. +``` + +## Constraints + +- The number of nodes in the tree is in the range `[1, 1000]`. +- `0 <= Node.val <= 1000` diff --git a/1029-vertical-order-traversal-of-a-binary-tree/vertical-order-traversal-of-a-binary-tree.ts b/leetcode/hard/0987-vertical-order-traversal-of-a-binary-tree/vertical-order-traversal-of-a-binary-tree.ts similarity index 58% rename from 1029-vertical-order-traversal-of-a-binary-tree/vertical-order-traversal-of-a-binary-tree.ts rename to leetcode/hard/0987-vertical-order-traversal-of-a-binary-tree/vertical-order-traversal-of-a-binary-tree.ts index 45d5d01..d07f60b 100644 --- a/1029-vertical-order-traversal-of-a-binary-tree/vertical-order-traversal-of-a-binary-tree.ts +++ b/leetcode/hard/0987-vertical-order-traversal-of-a-binary-tree/vertical-order-traversal-of-a-binary-tree.ts @@ -1,18 +1,15 @@ -/** - * 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 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[][] { +export function verticalTraversal(root: TreeNode | null): number[][] { // initialize a map where: // key = column index // value = array of [row, node.val] @@ -32,13 +29,13 @@ function verticalTraversal(root: TreeNode | null): number[][] { } traverse(root, 0, 0); - + // sort columns left -> right const sortedCols = Array.from(valueMap.keys()).sort((a, b) => a - b); return sortedCols.map((col) => { // sort column entries: row -> value - const sortedNodes = valueMap.get(col).sort((a, b) => a[0] - b[0] || a[1] - b[1]); + const sortedNodes = valueMap.get(col)!.sort((a, b) => a[0] - b[0] || a[1] - b[1]); return sortedNodes.map((n) => n[1]); }); -}; \ No newline at end of file +}