Accepted Java Solution


  • 0
    P
    public class Solution {
        public TreeNode sortedArrayToBST(int[] nums) {
        
            if(nums.length == 0)
                return null;
        
            int median = nums.length / 2;
        
            TreeNode node = new TreeNode(nums[median]);
        
            node.left = sortedArrayToBST(Arrays.copyOfRange(nums , 0 , median));
            node.right = sortedArrayToBST(Arrays.copyOfRange(nums , median + 1 ,     nums.length));
        
            return node;
        }
    }

Log in to reply
 

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