1ms Recursive Java solution


  • 2
    R
    public class Solution {
        public TreeNode sortedArrayToBST(int[] nums) {
            int low = 0, high = nums.length;
            return sortedArrayToBST(nums, low, high);
        }
        
        public TreeNode sortedArrayToBST(int[] nums, int low, int high) {
            TreeNode treeHead = null;
            if(low < high) {
                int mid = low + (high-low)/2;
                treeHead = new TreeNode(nums[mid]);
                treeHead.left = sortedArrayToBST(nums, low, mid);
                treeHead.right = sortedArrayToBST(nums, mid+1, high);
            } 
            return treeHead;
        }
    }

Log in to reply
 

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