BST to Sorted Circular Doubly Linked List
Updated
Problem statement
Given the root of a binary search tree with distinct values, convert the tree in place into a sorted circular doubly linked list and return the list as rows.
- Use each node's
leftpointer asprevious. - Use each node's
rightpointer asnext. - Link the nodes in ascending order.
- Join the smallest and largest nodes so the list is circular.
Do not create new list nodes. Return one row per node, in list order starting at the smallest node. Each row is [value, previousValue, nextValue]. For an empty tree, return an empty matrix. The returned rows do not count against the in-place rule.
Function
bstToCircularDoublyList(root: TreeNode) → int[][]Examples
Example 1
root = [4,2,5,1,3]return = [[1,5,2],[2,1,3],[3,2,4],[4,3,5],[5,4,1]]In sorted order the values are 1,2,3,4,5. The first node's previous pointer wraps around to 5, and the last node's next pointer wraps around to 1.
Example 2
root = []return = []The tree is empty, so the answer is an empty matrix.
Example 3
root = [7]return = [[7,7,7]]The only node's previous and next pointers both point back to itself.
Constraints
- The tree contains between
0and100000nodes. -1000000000 <= node.val <= 1000000000.- All node values are distinct.
- The input is a valid binary search tree.
- The conversion must reuse the original tree nodes. Recursion or your own stack for the traversal is allowed.
- The returned rows do not count against the in-place rule.
More Bloomberg LP Onsite Interview questions
- 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
- Decode StringPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Minimum Replacements to Make Two Strings AnagramsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 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
- Convert a Binary Tree to a Doubly Linked ListExpedia · ONSITE INTERVIEWExpedia interview questions
- Convert a BST to a Doubly Linked ListMicrosoft · ONSITE INTERVIEWMicrosoft 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