Difficulty: Easy, Asked-In: Google, Amazon, Cisco, VMWare.
Key takeaway: An excellent problem to learn problem-solving using recursive (divide and conquer) and iterative (using stack and queue) approaches.
Let’s understand the problem
Write a program to convert a sorted array of integers into a Balanced Binary Search Tree.
- Each node in the tree must follow the BST property: Values of all nodes in the left subtree are less than the value of given node, values of all nodes in the right subtree are greater than the value of given node, and both left and right subtrees are also BSTs.
- In a balanced binary tree, difference in the height of left and right subtrees does not exceed 1. In other words, height of the left and right subtrees are as close to equal as possible.
Example
Discussed solution approaches
- Recursive solution using divide and conquer
- Iterative solution using BFS traversal
- Iterative solution using stack
Recursive solution using divide and conquer
Solution idea
Our goal is to maintain two crucial properties of a binary search tree at every node: 1) BST property 2) The height balance property. To achieve this, we can use the property of a sorted array to construct the BST. How can we do this? Let's think!
If we select any value X[i] in the sorted array, then it is greater than all values from X[0] to X[i - 1] and less than all values from X[i + 1] to X[n - 1]. So, if we make X[i] the root of BST, we can allocate i number of nodes (X[0] to X[i - 1]) to the left subtree and n - i - 1 number of nodes (X[i + 1] to X[n - 1]) to the right subtree.
To ensure that the tree is balanced, the best approach would be to allocate equal number of nodes to both left and right subtrees. In other words, choosing the value at mid-index as the root would be the best choice. So the idea is based on divide and conquer : We divide the array into two equal parts and keep the middle element as root. After this, we recursively build BST for left and right subarray by calling the same function.
Solution steps
Suppose we define a function sortedArrayToBST(X[], l, r) to return the root of constructed BST. Here l is left end of array and r is the right end.
Divide: We divide the array into equal parts and create a new node as a root with the value X[mid].
Conquer: Both the left and right halves are smaller versions of the same problem i.e. constructing a balanced BST from a sorted array of size n/2. Left half = X[l] to X[mid - 1], Right half = X[mid + 1] to X[r].
- We recursively call the same function on the left half to build balanced left subtree.
- We recursively call the same function on the right half to build balanced right subtree.
Combine: After the completion of recursive calls, we will have a balanced BST. We will return the pointer to the root element.
Base case: This is the smallest version of the problem where recursion will terminate and directly return the solution. If we observe, the case of empty array i.e. l > r is the situation of base case.
Solution pseudocode
class BSTNode
data
left
right
BSTNode(value)
data = value
left = null
right = null
sortedArrayToBST(X, l, r)
if l > r
return null
mid = l + (r - l) / 2
root = new BSTNode(X[mid])
root.left = sortedArrayToBST(X, l, mid - 1)
root.right = sortedArrayToBST(X, mid + 1, r)
return rootTime and space complexity analysis
We are dividing the problem into two smaller sub-problems of equal size and recursively constructing the BST. Here time complexity of the divide and combine steps are O(1). So recurrence relation for time complexity is T(n) = 2T(n/2) + O(1). We can analyse it using the master theorem or recursion tree method.
Based on master theorem, if T(n) = aT(n/b) + O(n^k)
- Case 1: When k < logb(a) then T(n) = O(n^logb(a))
- Case 2: When k = logb(a) then T(n) = O(n^k * logn)
- Case 3: When k > logb(a) then T(n) = O(n^k)
Let's compare the above recurrence relation with master theorem recurrence:
- T(n) = aT(n/b) + O(n^k)
- T(n) = 2T(n/2) + O(n^0)
Here a = 2, b = 2 and k = 0. So logb(a) = log2(2) = 1, which is greater than k. So we can apply case 1 of the master theorem. Time complexity T(n) = O(n^logb(a)) = O(n^1) = O(n).
At each step of recursion, input size is decreasing by 1/2 and depth of the recursion tree will be O(logn). So space used by recursion call stack will be O(logn). Space complexity = O(logn).
Iterative solution using BFS Traversal
Solution idea
Another idea would be to construct a balanced binary search tree (BST) using level order traversal or breadth-first search. How do we do this? Let's think!
If we observe the balanced BST:
- Root node will be the middle element of array, which cover the entire range [l, r].
- Left child of root will be the middle value of subarray [l, mid - 1] and cover the range [l, mid - 1].
- Right child of root will be the middle value of subarray [mid + 1, r] and cover the range [mid + 1, r].
In this way, every node will cover some range of values and value of that node will be the middle element of that range. Now the critical question is: How do we build a balanced BST using this idea and level order traversal? Here are the steps.
Solution steps
One idea is clear: We need to track the range (left and right index) of each node so that we can find its left and right child during traversal. For this, we define a MyNode class which stores three things: BST node pointer, left range and right range of a given node. We will use queue to store MyNode.
class MyNode
{
BSTNode node
int l
int r
MyNode(BSTNode n, int lValue, int rValue)
{
node = n
l = lValue
r = rValue
}
}Step 1: The middle element of array is the root. So we create a BST node of this value and add it to the queue with the left and right indices of the range.
Step 2: Now we run a loop till queue is empty. Inside the loop, we first remove the root node from the queue and update left and right child of it: 1) Middle of left subarray [l, mid - 1] as the left child 2) Middle of right subarray [mid + 1, r] as the right child.
- We first create a BST node with the middle value of left subarray [l, mid - 1], update it as the left child of root and add it to the queue with updated left and right indices of its range.
- After this, we create a BST node with the middle value of right subarray [l, mid - 1], update it as the right child of root and add it to the queue with updated left and right indices of its range.
Step 3: Similarly, we keep removing nodes from the queue in the loop and updating left and right children based on the middle value of the node range. By end of the process, we return a pointer to the root node of the constructed tree.
Solution pseudocode
sortedArrayToBST(X, n)
if n == 0
return null
l = 0, r = n - 1
mid = l + (r - l) / 2
root = new BSTNode(X[mid])
queue = empty queue
enqueue MyNode(root, l, r)
while queue is not empty
current = front of queue
dequeue queue
mid = current.l + (current.r - current.l) / 2
// Build the left subtree if its range has at least two elements
if mid != current.l
leftMid = current.l + (mid - 1 - current.l) / 2
leftChild = new BSTNode(X[leftMid])
current.node.left = leftChild
enqueue MyNode(leftChild, current.l, mid - 1)
// Build the right subtree if its range has at least two elements
if mid != current.r
rightStart = mid + 1
rightMid = rightStart + (current.r - rightStart) / 2
rightChild = new BSTNode(X[rightMid])
current.node.right = rightChild
enqueue MyNode(rightChild, rightStart, current.r)
return rootTime and space complexity analysis
At each iteration of the while loop, we are removing one node from the queue, updating left and right child and adding both to the queue. In other words, we are performing constant number of operations at each iteration. So time complexity = n* O(1) = O(n).
Overall space complexity = Size of the queue + Extra space for each node to store left and right indices of the range = O(n) + O(n) = O(n).
Iterative solution using stack
Solution idea and steps
Similar to above approach, we can construct balanced BST using stack to keep track of nodes and indices. How can we do this? Let's understand!
Here we use stack S to stores BST nodes and two extra stack (leftIndexStack and rightIndexStack) to store left and right indices of the range.
Step 1: We first create root node and push it onto a stack S. We also push left and right indices (0 and n - 1) of range onto their respective stacks.
Step 2: Now we run a loop until the stack S is empty. At each iteration of the loop: We pop top node and indices from their respective stacks and choose middle element of the current subarray (defined by indices) as the value for the BST node.
- If left subarray is non-empty, we create a new left child for the current node. After this: We push left child, the left and right indices of the left subarray onto their respective stacks.
- If the right subarray is non-empty, we create a new right child for the current node. After this: We push right child, the left and right indices of the left subarray onto their respective stacks.
Step 3: By the end of loop, we return the root node of the constructed binary search tree.
Solution pseudocode
sortedArrayToBST(X, n)
if n == 0
return null
root = new BSTNode(0)
nodeStack = empty stack
leftIndexStack = empty stack
rightIndexStack = empty stack
push root onto nodeStack
push 0 onto leftIndexStack
push n - 1 onto rightIndexStack
while nodeStack is not empty
current = pop nodeStack
l = pop leftIndexStack
r = pop rightIndexStack
mid = l + (r - l) / 2
current.value = X[mid]
if l <= mid - 1
current.left = new BSTNode(0)
push current.left onto nodeStack
push l onto leftIndexStack
push mid - 1 onto rightIndexStack
if mid + 1 <= r
current.right = new BSTNode(0)
push current.right onto nodeStack
push mid + 1 onto leftIndexStack
push r onto rightIndexStack
return rootTime and space complexity analysis
We are adding and removing each node only once from the stack. Similarly, we are removing and adding left and right range for each node only once from their respective stacks. In simple words, we are performing constant number of operations at each iteration. So time complexity = n* O(1) = O(n).
Overall space complexity = Size of the stack to store BST nodes + Size of stack for storing left and right indices of the range = O(logn) + O(logn) = O(logn).
Critical ideas to think!
- Does the above solution work for both odd and even cases of input size?
- Can we solve this problem using in-order traversal?
- Can we solve this problem using constant extra space?
- How can we analyze the time complexity of the recursive solution using the recursion tree method?
- Why is the size of the queue O(n) in the BSF approach?
- Why is the size of the stack in the 3rd approach O(logn)?
- In the recursive solution, can we add a base condition for a one-size input to reduce the number of recursive calls?
- How can we modify the above code if the array is sorted in decreasing order?
Suggested coding problems to practice
- Convert sorted singly linked list to balanced BST.
- Convert sorted doubly linked list to balanced BST.
- Convert unbalanced BST to a balanced BST.
- Convert binary tree to a balanced binary search tree.
- Cover binary search tree to doubly linked list.
- Check that given binary tree is BST or not.
Please feel free to message below if you find any errors or want to share additional insights. Enjoy learning, enjoy algorithms!
