My 5 lines java recursion solution


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

Log in to reply
 

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