FastPrepBinary Tree Level Order
Problems/Binary Tree Level Order

Binary Tree Level Order

Morgan Stanley logoMorgan Stanley● MediumFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Morgan Stanley online assessment for a full-time role. The report names binary-tree level-order traversal. We chose the grouped output, what happens for an empty tree, and the number limits.

Our version is about 90% the same as the reported question.

Given the root of a binary tree, return its node values grouped by depth. The root forms the first row. Each following row contains the nodes one edge farther from the root, ordered from left to right.

Return an empty array for an empty tree. Equal values belong to distinct nodes and must all appear in the result.

Function

levelOrder(root: TreeNode) → List<List<Integer>>

Examples

Example 1

root = [4,2,7,null,3,6,9]return = [[4],[2,7],[3,6,9]]
root
423769

The root row is [4]. Its children form [2,7]. Node 2 has only right child 3, while node 7 has children 6 and 9; their next row is [3,6,9].

Example 2

root = [5,null,5,null,5]return = [[5],[5],[5]]
root
555

The three nodes form a right-child chain. Each has a different depth, so each contributes a separate one-value row even though their values are equal.

Constraints

  • For this exercise, assume the tree has between 0 and 500 nodes.
  • For this exercise, assume -1000000 <= node values <= 1000000.
  • The input is a valid finite binary tree.

More Morgan Stanley Online Assessment questions

  • Three-Resource 0/1 KnapsackOA · Seen Sep 2026
  • Maximum Concurrent ProcessesOA · ONSITE INTERVIEW · Seen Sep 2026
  • Maximum Total of Nonadjacent RatingsOA · Seen Jul 2026
  • Count Shipment SchedulesOA · Seen Apr 2026
  • Delete and EarnOA · Seen Apr 2026
  • Interleave Array HalvesOA · Seen Apr 2026
  • Get Patch SequenceOA · Seen Feb 2025
  • String Subsequences 🐳OA · Seen Mar 2024
  • Merge IntervalsOA · Seen Feb 2024
  • Format Chemical SymbolsOA · Seen Feb 2023

Similar questions at other companies

  • Maximum Data Transfer TimeMicrosoft · OAMicrosoft interview questions
  • Longest Transfer Time in a Server TreeRippling · OARippling interview questions
  • Longest Alternating a/b Path in a TreeHudson River Trading · OAHudson River Trading interview questions
  • Special Diameter EndpointsAgoda · OAAgoda interview questions
  • Delete Nodes and Return Forest RootsGoogle · ONSITE INTERVIEWGoogle interview questions
  • Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
  • Tree Height After Child PromotionSnowflake · PHONE SCREENSnowflake interview questions
  • Binary Tree Spiral Level OrderAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Check a Complete Binary TreeGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
  • Diameter of an Acyclic Undirected GraphSalesforce · ONSITE INTERVIEWSalesforce interview questions

Interview reports that mention this question

  • Morgan Stanley Intern Tech Rounds: 5 Experiences (Part 2 of 5)

Morgan Stanley interview experiences

  • Morgan Stanley Apprenticeship and Tech Interviews: 6 Experiences
  • Morgan Stanley New Grad Technology Analyst: 2 Experiences
  • Morgan Stanley Intern Oldest Loops: 5 Experiences (Part 5 of 5)
  • Morgan Stanley Intern OOP Loops: 5 Experiences (Part 4 of 5)
  • Morgan Stanley Intern Earlier Loops: 5 Experiences (Part 3 of 5)
All Morgan Stanley interview questionsSee Morgan Stanley hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        // Write your code here.
    }

}
Checking account…