Group Binary Tree Nodes Into Vertical Columns

Last updated: November 29, 2026

Quick Overview

Return the values of a binary tree grouped by vertical column from leftmost to rightmost. Within a column, list nodes top to bottom, and nodes on the same row and column in left-to-right order.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Return the values of a binary tree grouped by vertical column from leftmost to rightmost. Within a column, list nodes top to bottom, and nodes on the same row and column in left-to-right order.

Vertical order traversal is a well known Meta onsite question. The interviewer checks that you choose BFS (not DFS) so the top-to-bottom and left-to-right order falls out naturally, and that you avoid a final sort by tracking the column range.

What the Interviewer Expects
  • Chooses BFS so row order and left-to-right order are automatic
  • Assigns column -1 to left children and +1 to right children
  • Avoids sorting by tracking min and max column
  • Explains why plain DFS gets the order wrong
  • Handles an empty tree
Key Topics to Cover
BFS
Binary tree
Hash map
Level order traversal
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Now same-row, same-column nodes must be sorted by value. What changes?
  • Return only the top view of the tree.
  • How would you do it with DFS while still producing the right order?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: root of a binary tree, possibly empty. Output: list of columns, each a list of values. Order within a column: by depth, then by left-to-right position in BFS. Target O(n) time....

High-Level Design

Run BFS with a queue of (node, col). Append each popped value to columns[col] in a dict of lists. BFS visits rows top to bottom and each row left to right, which is exactly the required order with...


Submit Your Answer
Markdown supported