7-line java solution


  • 0

    Here is my Java solution, use the ideas of quick sorting.

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

Log in to reply
 

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