C# Solution


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

Log in to reply
 

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