1ms java solution


  • 0
    H

    public class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {

    	return sortedArrayToBST(nums,0,nums.length-1);
    }
    
    private TreeNode  sortedArrayToBST(int[] nums,int low, int high) {
        
    	if(low>=high) return null;
    	int mid = low + (high - low)/2;
    	TreeNode node = new TreeNode(nums[mid]);
    	node.left = sortedArrayToBST(nums,low,mid-1);
    	node.right = sortedArrayToBST(nums, mid+1, high);
    	
    	return node;
        
    }
    

    }


  • 0
    I

    This doesn't work, because of if(low>=high) return null;. Try for example [1] as input.


  • 0
    H

    change that condition to (low>high). In my submission i didn't submit with ">=" . It was just ">" . Don't remember how that "=" in here.


Log in to reply
 

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