Sorted Doubly Linked List to Balanced BST In Place
Updated
Problem statement
You are given values. values[i] is the value of node i of a doubly linked list. The nodes are linked in index order, and the values never go down. Reuse these same nodes to build a height-balanced binary search tree. Treat each node's previous pointer as its left child and its next pointer as its right child. Do not create new tree nodes.
Return one row per original node, in index order, as [nodeIndex, leftIndex, rightIndex, parentIndex]. Use -1 for a missing link. When a part of the list has an even number of nodes, pick the lower of the two middle nodes as its root. This makes the answer the same every time.
Function
buildBalancedBstLinks(values: int[]) → int[][]Examples
Example 1
values = [1,2,3,4]return = [[0,-1,-1,1],[1,0,2,-1],[2,-1,3,1],[3,-1,-1,2]]Four nodes is an even number, so the lower of the two middle nodes, node 1, becomes the root. Node zero is its left child. The right part holds nodes two and three. Its lower middle, node two, becomes the right child of the root, and node three is the right child of node two. Each row lists a node with its final links.
Example 2
values = [-10,-3,0,5,9]return = [[0,-1,1,2],[1,-1,-1,0],[2,0,3,-1],[3,-1,4,2],[4,-1,-1,3]]Five nodes have one middle, node 2, so it is the root. The left part holds nodes zero and one, and node zero becomes its root with node one as its right child. The right part holds nodes three and four, and node three becomes its root with node four as its right child.
Constraints
1 <= values.length <= 200000-1000000000 <= values[i] <= 1000000000valuesis nondecreasing.- The returned link table is the required serialization; it does not represent newly allocated tree nodes.
More Salesforce Onsite Interview questions
- Count Increasing TripletsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Reverse a Sublist of a Linked ListONSITE INTERVIEW · Seen Nov 2026
- Rotting OrangesONSITE INTERVIEW · Seen Nov 2026
- 3Sum SmallerONSITE INTERVIEW · Seen Nov 2026
- Count Zero Request ServersONSITE INTERVIEW · Seen Nov 2026
- Group Identical Tree LevelsONSITE INTERVIEW · Seen Oct 2026
- Meeting Rooms IIONSITE INTERVIEW · Seen Oct 2026
- Construct a Tree from Level-Order and Inorder TraversalsONSITE INTERVIEW · Seen Sep 2026
- Maximize Monsters DefeatedONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Convert a Sorted Linked List to a Balanced BSTMeta · PHONE SCREENMeta interview questions
- Path Between Two Nodes of a Fibonacci TreeDatabricks · PHONE SCREEN · ONSITE INTERVIEWDatabricks interview questions
- Maximum Root-to-Leaf and Leaf-to-Leaf Path SumsAmazon · ONSITE INTERVIEW
- Build a Tree from Preorder and Inorder TraversalsStackAdapt · ONSITE INTERVIEWStackAdapt interview questions
- Binary Tree Level Order TraversalGoogle · ONSITE INTERVIEWGoogle interview questions
- BST to Sorted Circular Doubly Linked ListApple · ONSITE INTERVIEWApple interview questions
- Lowest Common Ancestor Only When Both Targets ExistAdobe · ONSITE INTERVIEWAdobe interview questions
- Validate Binary Search TreeGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Balanced Tree Node ReportNetflix · ONSITE INTERVIEWNetflix interview questions
- Flatten a Multilevel Doubly Linked ListBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions