Binary Tree Level Order Traversal
Updated
Problem statement
Given the root of a binary tree, return its node values grouped by depth from top to bottom. Values within each level must appear from left to right. Return an empty list for an empty tree.
Function
levelOrder(root: TreeNode) → List<List<Integer>>Examples
Example 1
root = [3,9,20,null,null,15,7]return = [[3],[9,20],[15,7]]The root forms level zero, its two children form level one, and nodes 15 and 7 form level two.
Example 2
root = [1,null,2,3]return = [[1],[2],[3]]Each node occupies a separate depth even though the tree is skewed.
Example 3
root = []return = []An empty tree has no levels.
Constraints
- The tree contains at most
100000nodes. - Each node value is between
-10^9and10^9.
More Google Onsite Interview questions
- 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
- Route Pattern MatcherPHONE SCREEN · ONSITE 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
- Diagonal Traversal of a Binary TreeAmazon · ONSITE INTERVIEW
- Diameter of an Acyclic Undirected GraphSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Lowest Common Ancestor Only When Both Targets ExistAdobe · ONSITE INTERVIEWAdobe interview questions
- Sum Root-to-Leaf NumbersMeta · ONSITE INTERVIEWMeta interview questions
- Validate Binary Search TreeGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Balanced Tree Node ReportNetflix · ONSITE INTERVIEWNetflix interview questions
- Binary Tree Spiral Level OrderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Binary Tree Zigzag TraversalExpedia · ONSITE INTERVIEWExpedia interview questions
- Top View of a Binary TreeInMobi · ONSITE INTERVIEWInMobi interview questions
- Ternary Expression to Binary TreeNutanix · ONSITE INTERVIEWNutanix interview questions