Simple Java 1ms


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

Log in to reply
 

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