Convert a BST to a Doubly Linked List
Updated
Problem statement
Given a binary search tree root, convert its nodes into a linear doubly linked list in ascending key order.
Return a portable representation of the list: row i is [value, prevIndex, nextIndex], rows appear from head to tail, and -1 represents a missing neighbor. Return an empty matrix for an empty tree.
Function
bstToDoublyLinkedList(root: TreeNode) → int[][]Examples
Example 1
root = [4,2,5,1,3]return = [[1,-1,1],[2,0,2],[3,1,3],[4,2,4],[5,3,-1]]In-order traversal yields 1,2,3,4,5; the second and third columns encode each node's previous and next list indexes.
Example 2
root = []return = []An empty tree produces an empty list representation.
Constraints
- The tree contains at most
100000nodes -10^9 <= node.val <= 10^9- All node values are distinct
- The input satisfies the binary-search-tree ordering property
More Microsoft Onsite Interview questions
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- BST to Sorted Circular Doubly Linked ListGoogle · ONSITE INTERVIEWGoogle interview questions
- BST to Sorted Circular Doubly Linked ListApple · ONSITE INTERVIEWApple interview questions
- BST to Sorted Circular Doubly Linked ListBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Convert a Binary Tree to a Doubly Linked ListExpedia · ONSITE INTERVIEWExpedia interview questions
- Turn a BST Into a Sorted Doubly Linked ListAdobe · PHONE SCREENAdobe interview questions
- Diagonal Traversal of a Binary TreeAmazon · ONSITE INTERVIEW
- Render an Org Chart and Find Skip-Level PairsSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Find Leaves of a Binary TreeLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions