
Problem
Solution
Submissions
Sorted Array to Binary Search Tree
Certification: Basic Level
Accuracy: 40%
Submissions: 5
Points: 5
Write a Java program to convert a sorted array to a height-balanced binary search tree (BST).
Example 1
- Input: nums = [-10,-3,0,5,9]
- Output: [0,-3,9,-10,null,5]
- Explanation:
- BST structure:
- BST structure:
Example 2
- Input: nums = [1,3]
- Output: [3,1]
- Explanation:
- BST structure:
- BST structure:
Constraints
- 1 ≤ nums.length ≤ 10^4
- -10^4 ≤ nums[i] ≤ 10^4
- nums is sorted in strictly increasing order
- Time Complexity: O(n)
- Space Complexity: O(log n)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use a recursive approach to build the BST.
- Choose the middle element of the array as the root.
- Recursively build left subtree from elements before the middle element.
- Recursively build right subtree from elements after the middle element.
- Return the root of the tree.