Advertisement

Vertical Order Traversal of a Binary Tree - LeetCode 987 Solution

Vertical Order Traversal of a Binary Tree - Complete Solution Guide

Vertical Order Traversal of a Binary Tree is LeetCode problem 987, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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

Detailed Explanation

The problem asks us to perform a vertical order traversal of a binary tree. Each node in the tree has a coordinate (row, col), with the root at (0, 0). A node's left child is at (row+1, col-1), and its right child is at (row+1, col+1). We need to return a list of lists, where each inner list contains the nodes in a particular column, sorted from top to bottom. If nodes are in the same row and column, they should be sorted by their values.

Solution Approach

The solution uses a BFS traversal to visit each node in the tree, keeping track of the row and column of each node. A hash map (column_table) is used to store a list of (row, value) pairs for each column. After traversing the entire tree, the keys of the hash map (representing the columns) are sorted. For each column, the (row, value) pairs are sorted based on row first and then by value. Finally, the sorted values are extracted and added to the result list.

Step-by-Step Algorithm

  1. Step 1: Initialize a queue for BFS traversal and a hash map (column_table) to store nodes based on column number.
  2. Step 2: Enqueue the root node with coordinates (0, 0).
  3. Step 3: While the queue is not empty:
  4. Step 4: Dequeue a node (node, row, col) from the queue.
  5. Step 5: Store the (row, node.val) in the column_table at key 'col'.
  6. Step 6: Enqueue the left child (if it exists) with coordinates (row+1, col-1).
  7. Step 7: Enqueue the right child (if it exists) with coordinates (row+1, col+1).
  8. Step 8: After the BFS is complete, sort the keys (column numbers) of the column_table.
  9. Step 9: Iterate through the sorted column numbers:
  10. Step 10: Sort the (row, value) pairs in the current column based on row (ascending) and value (ascending).
  11. Step 11: Extract the values from the sorted pairs and add them to a list representing the column's vertical order.
  12. Step 12: Add the list of values for the current column to the result list.
  13. Step 13: Return the result list.

Key Insights

  • Insight 1: We can use a Breadth-First Search (BFS) to traverse the tree level by level, assigning coordinates to each node as we visit them.
  • Insight 2: A hash map (or map in C++) is essential to store nodes based on their column number. This allows us to easily group nodes in the same column.
  • Insight 3: We need to sort the nodes within each column based on their row and value. This requires using a custom sorting function or lambda expression.

Complexity Analysis

Time Complexity: O(W*H*log(H))

Space Complexity: O(N)

Topics

This problem involves: Hash Table, Tree, Depth-First Search, Breadth-First Search, Sorting, Binary Tree.

Companies

Asked at: Deliveroo, DoorDash, Samsung, eBay.