Java 1ms recursive solution


  • 0
    R
    public TreeNode sortedArrayToBST(int[] nums) {
    		return arrayToBSTHelper(nums, 0, nums.length-1);
    	}
    	
    	private TreeNode arrayToBSTHelper(int[] nums, int start, int end) {
    		if (start > end)
    			return null;
    		int mid = (start + end) / 2;
    		TreeNode root = new TreeNode(nums[mid]);
    		
    		root.left = arrayToBSTHelper(nums, start, mid - 1);
    		root.right = arrayToBSTHelper(nums, mid + 1, end);	
    		
    		return root;
    	}
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.