Leetcode: Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

public class Solution {
 public TreeNode sortedArrayToBST(int[] num) {
   return sortedArrayToBST(num, 0, num.length-1);
 public TreeNode sortedArrayToBST(int[] num, int start, int end){
     return null;
   int pivot = (start+end)/2;
   TreeNode root = new TreeNode(num[pivot]);
   root.left = sortedArrayToBST(num, start, pivot-1);
   root.right = sortedArrayToBST(num, pivot+1, end);
   return root;



Leave a Reply

Your email address will not be published. Required fields are marked *