Rebuild Binary Tree from Level Order
Updated
Problem statement
You are given levelOrder, a binary tree written out level by level, top to bottom and left to right. The first value is the root. Then go through the real nodes in the order they appear, one at a time. For each one, the next unused value is its left child and the value after that is its right child. A value equal to nullMarker means that child is missing. nullMarker is never the value of a real node. Missing children at the very end of the list may be left out.
Rebuild the tree. Return its values in preorder: a node first, then its whole left subtree, then its whole right subtree. Write nullMarker for every missing left or right child.
Function
levelOrderToPreorder(levelOrder: int[], nullMarker: int) → int[]Examples
Example 1
levelOrder = [1,2,3,-1,4,-1,5]nullMarker = -1return = [1,2,-1,4,-1,-1,3,-1,5,-1,-1]Node 1 has children 2 and 3. Node 2 has no left child, and its right child is 4. Node 3 has no left child, and its right child is 5. The list ends there, so the last two nodes have no children. In preorder, each missing child is written as -1.
Constraints
1 <= levelOrder.length <= 100000.- The list is always valid, and the rule above uses up every value in it.
- Every real node value differs from
nullMarker. - If the root equals
nullMarker, it is the only value in the list.
More Bloomberg LP Onsite Interview questions
- Pascal's TrianglePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Valid Triangle NumberPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Collatz Conjecture with Shared MemoizationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent WordsONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Diagonal Traversal of a Binary TreeAmazon · ONSITE INTERVIEW
- Delete Nodes and Return Forest RootsGoogle · ONSITE INTERVIEWGoogle interview questions
- Diameter of an Acyclic Undirected GraphSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Binary Tree Zigzag TraversalExpedia · ONSITE INTERVIEWExpedia interview questions
- Top View of a Binary TreeInMobi · ONSITE INTERVIEWInMobi interview questions
- Maximum Value at Each Tree LevelMeta · ONSITE INTERVIEWMeta interview questions
- Diameter of Binary TreeeBay · ONSITE INTERVIEWeBay interview questions
- Maximum Depth of Binary TreeLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
- Symmetric Binary TreeFigma · ONSITE INTERVIEWFigma interview questions
- Mirror a Binary TreeAMD · ONSITE INTERVIEWAMD interview questions
Bloomberg LP interview experiences
- Bloomberg AI-Assisted Coding Round, All Levels: 14 Experiences
- Bloomberg SWE Intern Interview Questions: 68 Questions
- Bloomberg Interview Questions, Level Not Reported: 65 Questions
- Bloomberg Experienced Design & More: 126 Questions (Part 2 of 2)
- Bloomberg Experienced SWE Interviews: 167 Questions (Part 1 of 2)
- Bloomberg New Grad Coding Questions: 121 Questions (Part 2 of 2)