As reported: Salesforce SMTS loop (Bangalore, Aug 2025), round 1 (DSA): two questions that both had to pass all test cases — Longest Substring Without Repeating Characters, then this one. The discussion touched on the extra
O(log N)from keeping columns in an ordered map.
This is LeetCode 987 — Vertical Order Traversal of a Binary Tree.
Place the root at (row 0, column 0); a node at (r, c) has its left child at (r + 1, c − 1) and its right child at (r + 1, c + 1). Return the node values column by column from the leftmost column to the rightmost. Within a column, list nodes from top to bottom; nodes in the same row and column are listed in increasing order of value.
Example 1
root = [3,9,20,null,null,15,7]
Output: [[9],[3,15],[20],[7]]
Example 2
root = [1,2,3,4,5,6,7]
Output: [[4],[2],[1,5,6],[3],[7]]
5 and 6 share row 2, column 0, so they are sorted by value.
Constraints: 1 ≤ number of nodes ≤ 1000; 0 ≤ Node.val ≤ 1000.
Code runner format: one line: root in LeetCode's level-order format. Implement verticalTraversal(root); the harness prints the columns.