Convert a Sorted Linked List to a Balanced BST
Updated
Problem statement
You are given the head of a singly linked list whose values are in strictly increasing order. Build a height-balanced binary search tree that holds every value of the list exactly once, and return its root.
In a binary search tree, every value in a node's left subtree is smaller than the node's value, and every value in its right subtree is larger. A tree is height-balanced when, at every node, the heights of the left and right subtrees differ by at most one.
Many trees meet these rules, but only one is accepted: the tree built this way. The root of any part of the list is the middle node of that part. If the part has an even number of nodes, it has two middle nodes; take the one closer to the front. The nodes before the root form its left subtree and the nodes after it form its right subtree, and each is built by the same rule. An empty list gives an empty tree.
In the examples, a tree is written level by level from left to right, with null marking a missing child; nulls after the last node are left out.
Function
sortedListToBST(head: ListNode) → TreeNodeExamples
Example 1
head = [-7,-2,4,8,15]return = [4,-7,8,null,-2,null,15]The list has five nodes, so its middle node 4 is the root. The nodes before it, -7, -2, are an even pair, so the one closer to the front, -7, roots the left subtree and -2 becomes its right child. In the same way 8 roots the right subtree and 15 is its right child. In level order this is [4,-7,8,null,-2,null,15].
Example 2
head = [1,2,3,4]return = [2,1,3,null,null,null,4]Four nodes have two middle nodes, 2 and 3. The rule picks the one closer to the front, so 2 is the root. 1 is its left child. The right part, 3, 4, is again even, so 3 is its root and 4 hangs on its right.
Example 3
head = []return = []An empty list gives an empty tree, shown as [].
Constraints
- The list has between
0and1000nodes. -10^5 <= node.val <= 10^5- The values are in strictly increasing order.
More Meta Phone Screen questions
- Fuzzy Word Lookup With One Misspelled LetterPHONE SCREEN · Seen Nov 2026
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Run-Length Encoded Vector Dot ProductPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockPHONE SCREEN · Seen Nov 2026
- First Element Greater Than KPHONE SCREEN · Seen Nov 2026
- Count Events in Rolling Minute WindowsPHONE SCREEN · Seen Oct 2026
- Valid Word AbbreviationPHONE SCREEN · Seen Oct 2026
- Shortest Path in a Binary MatrixPHONE SCREEN · Seen Sep 2026
- Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Sorted Doubly Linked List to Balanced BST In PlaceSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
- Construct a Binary Tree from Preorder and InordereBay · PHONE SCREENeBay interview questions
- Filter Data with Composable PredicatesCoreWeave · PHONE SCREEN
- Trim a Binary Search Tree to a RangeGoogle · PHONE SCREENGoogle interview questions
- Count Uni-Valued SubtreesAmazon · PHONE SCREEN
- Reconstruct a BST from PreorderLyft · PHONE SCREENLyft interview questions
- Flatten a Multilevel Doubly Linked ListBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Rebuild a Binary Tree From Two WalksAdobe · PHONE SCREENAdobe interview questions
- Swap Adjacent Linked List NodesNutanix · PHONE SCREENNutanix interview questions
Meta interview experiences
- Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
- Meta Business Support Engineer Phone Screen: 1 Experience
- Meta Software Engineer Onsite Questions: 3 Experiences
- Meta AI Native Engineer OA and Onsite: 1 Experience
- Meta AI-Native Coding Interview: 3 Experiences
- Meta Production Engineer Intern Questions: 6 Experiences