
Problem
Solution
Submissions
Sorted Array to a Balanced Binary Search Tree.
Certification: Intermediate Level
Accuracy: 0%
Submissions: 0
Points: 12
Write a C# program to convert a sorted array to a balanced binary search tree (BST). A height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differs by more than 1.
Example 1
- Input: nums = [-10, -3, 0, 5, 9]
- Output: [0, -3, 9, -10, null, 5]
- Explanation:
- The balanced BST would look like:
- The balanced BST would look like:
Example 2
- Input: nums = [1, 3]
- Output: [3, 1]
- Explanation:
- The balanced BST would look like:
- The balanced BST would look like:
Constraints
- 1 <= nums.length <= 10^4
- -10^4 <= nums[i] <= 10^4
- nums is sorted in ascending 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 recursion to build the balanced BST
- Choose the middle element of the array as the root
- Recursively build left subtree with elements before the middle
- Recursively build right subtree with elements after the middle
- Return the root of the balanced BST