Algo-Sim › Binary Trees & BST
Convert Sorted Array to Binary Search Tree
Easy · Binary Trees & BST · step through it in Java, Python and C++ on your own input
Take the middle value as the root and recurse on each half, so every subtree splits its values evenly and stays height-balanced.
Problem
Given an integer array nums sorted in strictly increasing order, build a height-balanced binary search tree from it (every node’s two subtrees differ in height by at most one). This version always picks the lower middle, so the answer is unique.
Constraints
- 1 ≤ nums.length ≤ 10⁴
- -10⁴ ≤ nums[i] ≤ 10⁴
numsis strictly increasing
Edge cases to test
- Empty array
- One element
- Even length (either middle works)
Follow-up questions
- What about a sorted linked list?
- Build in inorder: recurse on the left half by count, take the current list node as the root, then build the right: O(n) (LC 109).
- Why is the result height-balanced?
- Each subtree gets half the values (±1), so its height is ⌈log₂(n + 1)⌉.
Solution
Java
public TreeNode sortedArrayToBST(int[] nums) {
return build(nums, 0, nums.length - 1);
}
private TreeNode build(int[] nums, int lo, int hi) {
if (lo > hi) return null;
int mid = lo + (hi - lo) / 2;
TreeNode node = new TreeNode(nums[mid]);
node.left = build(nums, lo, mid - 1);
node.right = build(nums, mid + 1, hi);
return node;
}Python
def sorted_array_to_bst(nums):
return build(nums, 0, len(nums) - 1)
def build(nums, lo, hi):
if lo > hi:
return None
mid = lo + (hi - lo) // 2
node = TreeNode(nums[mid])
node.left = build(nums, lo, mid - 1)
node.right = build(nums, mid + 1, hi)
return nodeC++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
TreeNode *sortedArrayToBST(vector<int> &nums) {
return build(nums, 0, int(nums.size()) - 1);
}
private:
TreeNode *build(vector<int> &nums, int lo, int hi) {
if (lo > hi)
return nullptr;
int mid = lo + (hi - lo) / 2;
TreeNode *node = new TreeNode(nums[mid]);
node->left = build(nums, lo, mid - 1);
node->right = build(nums, mid + 1, hi);
return node;
}
};
More Binary Trees & BST problems
- Preorder Traversal (Recursive) (Easy)
- Inorder Traversal (Recursive) (Easy)
- Postorder Traversal (Recursive) (Easy)
- Preorder Traversal (Iterative) (Easy)
- Inorder Traversal (Iterative) (Easy)
- Postorder Traversal (Iterative) (Easy)
- Binary Tree Level Order Traversal (Medium)
- Binary Tree Zigzag Level Order Traversal (Medium)
- Binary Tree Right Side View (Medium)
- Vertical Order Traversal of a Binary Tree (Hard)
- Maximum Depth of Binary Tree (Easy)
- Diameter of Binary Tree (Easy)
- Balanced Binary Tree (Easy)
- Invert Binary Tree (Easy)
- Flatten Binary Tree to Linked List (Medium)
- Same Tree (Easy)
- Symmetric Tree (Easy)
- Subtree of Another Tree (Easy)
- Path Sum (Easy)
- Path Sum II (Medium)
- Path Sum III (Medium)
- Count Good Nodes in Binary Tree (Medium)
- Lowest Common Ancestor of a Binary Tree (Medium)
- Kth Ancestor of a Tree Node (Hard)
- All Nodes Distance K in Binary Tree (Medium)
- Validate Binary Search Tree (Medium)
- Search in a Binary Search Tree (Easy)
- Insert into a Binary Search Tree (Medium)
- Delete Node in a BST (Medium)
- Kth Smallest Element in a BST (Medium)
- Binary Search Tree Iterator (Medium)
- AVL Tree Insert (Hard)
- Construct Binary Tree from Preorder and Inorder Traversal (Medium)
- Binary Tree Maximum Path Sum (Hard)
- Serialize and Deserialize Binary Tree (Hard)