Very Simple Java Solution

  • 5
    public class Solution {
      public TreeNode sortedArrayToBST(int[] nums) {
        return helper(nums, 0, nums.length-1);
      private TreeNode helper(int[] nums, int l, int r){
        if (l>r) return null;
        if (l==r) return new TreeNode(nums[l]);
        int mid = (l+r)/2;
        TreeNode root = new TreeNode(nums[mid]);
        root.left = helper(nums, l, mid-1);
        root.right = helper(nums, mid+1, r);
        return root;

Log in to reply

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