Similar Problems

Similar Problems not available

Convert Sorted List To Binary Search Tree - Leetcode Solution

Companies:

LeetCode:  Convert Sorted List To Binary Search Tree Leetcode Solution

Difficulty: Medium

Topics: binary-search-tree linked-list binary-tree tree  

Problem statement: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

Solution: To solve this problem, we can follow the below approach:

  1. First, we need to find the middle element of the linked list.
  2. Then we can create a TreeNode with the middle element.
  3. We can then recursively create the left and right subtrees using the nodes before and after the middle element in the linked list.
  4. We can repeat the above steps until the entire list is used up or we have no more nodes to create subtrees.

Below is the code implementation in Python:

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
 
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
        
class Solution:
    def sortedListToBST(self, head: ListNode) -> TreeNode:
        if not head:
            return None
        if not head.next:
            return TreeNode(head.val)
        
        # find middle node using slow and fast pointer
        slow, fast = head, head.next.next
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        
        # create a TreeNode with the middle value
        root = TreeNode(slow.next.val)
        
        # recursively create left and right subtrees
        rightHead = slow.next.next
        slow.next = None
        root.left = self.sortedListToBST(head)
        root.right = self.sortedListToBST(rightHead)
        
        return root

The time complexity of the above code is O(n log n), where n is the number of nodes in the linked list. This is because we need to find the middle element using the slow and fast pointer, which takes O(n) time. And then for each subtree, we need to recursively process half the number of nodes, which also takes O(n) time. Hence the overall time complexity is O(n log n).

The space complexity of the above code is O(log n), which is the space required for the recursion stack. This is because for each level of the recursion tree, we only need to store the root of the subtree.

Test Cases: Let's consider a few test cases to validate our solution:

Example 1: Input: head = [-10,-3,0,5,9] Output: [0,-3,9,-10,null,5] Explanation: One possible answer is [0,-3,9,-10,null,5], which represents the following BST: 0 /
-3 9 / / -10 5

Example 2: Input: head = [1,3] Output: [3,1] Explanation: One possible answer is [3,1], which represents the following BST: 3
1

Example 3: Input: head = [] Output: []

In conclusion, we have successfully solved the Convert Sorted List to Binary Search Tree problem on Leetcode using a recursive approach.

Convert Sorted List To Binary Search Tree Solution Code

1