Skip to content
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
File renamed without changes.
57 changes: 0 additions & 57 deletions 1029-vertical-order-traversal-of-a-binary-tree/README.md

This file was deleted.

8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Count problem directories by difficulty
for level in easy medium hard; do
  count=$(find "leetcode/$level" -mindepth 1 -maxdepth 1 -type d 2>/dev/null | wc -l)
  echo "$level: $count"
done
echo "total: $(find leetcode -mindepth 2 -maxdepth 2 -type d 2>/dev/null | wc -l)"

Repository: pertrai1/coding-challenges

Length of output: 109


🌐 Web query:

LeetCode problem number "Vertical Order Traversal of a Binary Tree"

💡 Result:

LeetCode problem number: 987Vertical Order Traversal of a Binary Tree [1][3]


Correct the LeetCode problem ID and update the total count.

The problem ID for "Vertical Order Traversal of a Binary Tree" is 987, not 1029. Additionally, the LeetCode total on line 9 should be 173, not 172, to match the actual number of problems in the directory structure (81 easy + 77 medium + 15 hard).

🤖 Prompt for AI Agents
In `@README.md` at line 9, Update the LeetCode entry in the README table: change
the problem ID for "Vertical Order Traversal of a Binary Tree" from 1029 to 987
and increment the LeetCode total count from 172 to 173 so the table row "|
[LeetCode](`#leetcode`)           | Data Structures & Algorithms | 172            
|" reflects the corrected ID and total.

| [GreatFrontEnd](#greatfrontend) | Frontend Engineering | 15 |

## Platforms
Expand Down Expand Up @@ -287,14 +287,18 @@ Various places where I enjoy practicing solving coding challenges:
</details>

<details>
<summary>Hard Problems (14 solved)</summary>
<summary>Hard Problems (15 solved)</summary>

### Array & Dynamic Programming

- [0115 - Distinct Subsequences](./leetcode/hard/0115-distinct-subsequences) ![Hard](https://img.shields.io/badge/Hard-red)
- [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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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`
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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]);
});
};
}