LeetCode #109 Convert Sorted List to Binary Search Tree

Problem

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

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Example

1
2
3
4
5
6
7
8
9
Given the sorted linked list: [-10,-3,0,5,9],

One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:

0
/ \
-3 9
/ /
-10 5

Solution1

Method: Divide and Conquer

Time Complexity: O(n log n)

Space Complexity: O(n)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
def sortedListToArray(self, head):
"""
:type head: ListNode
:rtype: array
"""
ret = []
while head:
ret.append(head.val)
head = head.next

return ret

def sortedListToBST(self, head):
"""
:type head: ListNode
:rtype: TreeNode
"""
if not head:
return []

array = self.sortedListToArray(head)
tree_head = self.helper(array)

return tree_head

def helper(self, array):
"""
:type array: array
:rtype: TreeNode
"""
if not array:
return None
elif len(array) == 1:
return TreeNode(array[0])
elif len(array) == 2:
tree_node = TreeNode(array[0])
tree_node.right = TreeNode(array[1])
return tree_node

mid = len(array) //2
tree_node = TreeNode(array[mid])
tree_node.left = self.helper(array[:mid])
if mid + 1 < len(array):
tree_node.right = self.helper(array[mid + 1:])

return tree_node

or

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
def sortedListToBST(self, head):
if not head:
return
if not head.next:
return TreeNode(head.val)

# here we get the middle point,
# even case, like '1234', slow points to '2',
# '3' is root, '12' belongs to left, '4' is right
# odd case, like '12345', slow points to '2', '12'
# belongs to left, '3' is root, '45' belongs to right
slow, fast = head, head.next.next

while fast and fast.next:
fast = fast.next.next
slow = slow.next

# tmp points to root
tmp = slow.next

# cut down the left child
slow.next = None
root = TreeNode(tmp.val)
root.left = self.sortedListToBST(head)
root.right = self.sortedListToBST(tmp.next)

return root

Reference

https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/35474/Python-recursive-solution-with-detailed-comments-(operate-linked-list-directly).