Binary Tree Vertical Order Traversal
Updated
Problem statement
Given the root of a binary tree, return its vertical order traversal.
Place the root in column 0. A left child is one column to the left of its parent, and a right child is one column to the right. Return the columns from leftmost to rightmost.
Within each column, list nodes from top to bottom. If two nodes have the same row and column, preserve their left-to-right breadth-first encounter order.
Function
verticalOrder(root: TreeNode) → int[][]Examples
Example 1
root = [3,9,20,null,null,15,7]return = [[9],[3,15],[20],[7]]The occupied columns are -1, 0, 1, and 2. Nodes 3 and 15 share column 0 and appear from top to bottom.
Example 2
root = [1,2,3,4,5,6,7]return = [[4],[2],[1,5,6],[3],[7]]Nodes 5 and 6 have the same row and column. Breadth-first left-to-right order places 5 before 6.
Example 3
root = []return = []An empty tree has no columns.
Constraints
- The tree contains at most
1000nodes. -1000 <= Node.val <= 1000.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Vertical Order Traversal of a Binary TreeAmazon · ONSITE INTERVIEW
- Binary Tree Vertical Order TraversalBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Group Identical Tree LevelsSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Top-Down Employee Reporting ForestOkta · ONSITE INTERVIEWOkta interview questions
- Merge N-Ary Trees by KeyLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
- Filesystem Hash TreeCursor · PHONE SCREEN · ONSITE INTERVIEWCursor interview questions
- Bottom View of a Binary TreeOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions
- Binary Tree Vertical Order with Stable TiesMeta · ONSITE INTERVIEWMeta interview questions