C# solution


  • 0
    T
    public TreeNode SortedArrayToBST(int[] nums)
            {
                if (nums.Length == 0)
                {
                    return null;
                }
    
                return SortedArrayToBST(nums, 0, nums.Length - 1);
            }
    
            private TreeNode SortedArrayToBST(int[] nums, int startIndex, int endIndex)
            {
                int count = endIndex - startIndex + 1;
                int middle = count % 2 == 0 ? count / 2 : count / 2 + 1;
                middle = middle - 1;
    
                int effectiveIndex = startIndex + middle;
                TreeNode node = new TreeNode(nums[effectiveIndex]);
    
                if (effectiveIndex - 1 >= startIndex)
                {
                    node.left = SortedArrayToBST(nums, startIndex, effectiveIndex - 1);
                }
    
                if (effectiveIndex + 1 <= endIndex)
                {
                    node.right = SortedArrayToBST(nums, effectiveIndex + 1, endIndex);
                }
    
                return node;
            }
    

Log in to reply
 

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