C# - recursive


  • 0
    public TreeNode SortedArrayToBST(int[] nums) {
        return Partition(nums, 0, nums.Length - 1);
    }
    
    public TreeNode Partition(int[] nums, int left, int right)
    {
        if (right < left) return null;
        int mid = (left + right)/2;
        TreeNode root = new TreeNode(nums[mid]);
        root.left = Partition(nums, left, mid - 1);
        root.right = Partition(nums, mid + 1, right);
        
        return root;
    }

Log in to reply
 

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