Similar Problems
Similar Problems not available
Convert Sorted Array To Binary Search Tree - Leetcode Solution
LeetCode: Convert Sorted Array To Binary Search Tree Leetcode Solution
Difficulty: Easy
Topics: binary-search-tree tree binary-tree array
Problem Statement: Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.
A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one.
Solution Approach: To solve this problem, we need to build a balanced binary search tree from the given sorted array. Since the array is sorted in ascending order, we can always look for the middle element of the array and make it the root of the binary search tree. Then, we can recursively build the left and right sub-trees of the root node using the left and right half of the array respectively.
To implement this approach, we can define a recursive function named sortedArrayToBST that takes in the sorted array and the starting and ending index of the array as parameters. In each recursive call, we can look for the middle element of the sub-array and make it the root of the subtree. Then, we can recursively call the sortedArrayToBST function for the left and right halves of the sub-array and attach them to the left and right pointers of the root node respectively.
Algorithm:
- Define a recursive function named sortedArrayToBST that takes in the sorted array nums, the starting index lo, and the ending index hi as parameters.
- If the starting index lo is greater than the ending index hi, return null.
- Compute the middle index mid as (lo + hi) / 2.
- Create a new TreeNode node with the value nums[mid].
- Recursively call the sortedArrayToBST function for the left sub-array with lo index as lo and hi index as mid - 1 and attach the returned subtree to the left pointer of the node.
- Recursively call the sortedArrayToBST function for the right sub-array with lo index as mid + 1 and hi index as hi and attach the returned subtree to the right pointer of the node.
- Return the node.
Let's implement it in code:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
if (nums == null || nums.length == 0) {
return null;
}
return sortedArrayToBST(nums, 0, nums.length - 1);
}
private TreeNode sortedArrayToBST(int[] nums, int lo, int hi) {
if (lo > hi) {
return null;
}
int mid = (lo + hi) / 2;
TreeNode node = new TreeNode(nums[mid]);
node.left = sortedArrayToBST(nums, lo, mid - 1);
node.right = sortedArrayToBST(nums, mid + 1, hi);
return node;
}
}
The time complexity of this approach is O(n) because we are processing each element of the array only once. The space complexity is also O(n) because we are creating an additional TreeNode for each element of the array.
Convert Sorted Array To Binary Search Tree Solution Code
1