Number of Ways to Reorder Array to Get Same BST Problem
Description
LeetCode Problem 1569.
Given an array nums that represents a permutation of integers from 1 to n. We are going to construct a binary search tree (BST) by inserting the elements of nums in order into an initially empty BST. Find the number of different ways to reorder nums so that the constructed BST is identical to that formed from the original array nums.
- For example, given
nums = [2,1,3], we will have 2 as the root, 1 as a left child, and 3 as a right child. The array[2,3,1]also yields the same BST but[3,2,1]yields a different BST.
Return the number of ways to reorder nums such that the BST formed is identical to the original BST formed from nums.
Since the answer may be very large, return it modulo 10^9 + 7.
Example 1:

Input: nums = [2,1,3]
Output: 1
Explanation: We can reorder nums to be [2,3,1] which will yield the same BST. There are no other ways to reorder nums which will yield the same BST.
Example 2:

Input: nums = [3,4,5,1,2]
Output: 5
Explanation: The following 5 arrays will yield the same BST:
[3,1,2,4,5]
[3,1,4,2,5]
[3,1,4,5,2]
[3,4,1,2,5]
[3,4,1,5,2]
Example 3:

Input: nums = [1,2,3]
Output: 0
Explanation: There are no other orderings of nums that will yield the same BST.
Constraints:
1 <= nums.length <= 10001 <= nums[i] <= nums.length- All integers in
numsare distinct.
Solution
Approach: Divide at the Root and Count Interleavings
The first value of the array is always the root. Every value smaller than the root ends up in the left subtree and every larger value in the right subtree, and the two subtrees never affect each other. So an order builds the same BST exactly when:
- it starts with the same root,
- the left values, read in the order they appear, build the same left subtree,
- the right values, read in the order they appear, build the same right subtree.
How the left and right values are interleaved does not matter. If the left subtree has L values and the right one R, there are C(L + R, L) ways to choose which of the L + R positions after the root hold left values. Let ways(seq) be the number of good orders of seq, counting seq itself:
ways(seq) = C(L + R, L) * ways(left) * ways(right)
with ways = 1 for fewer than three values (there is only one way to order a root and at most one child). The answer is ways(nums) - 1, since the original order does not count as a reordering.
The binomial coefficients get huge, so build Pascal's triangle modulo 10^9 + 7 up front with C(a, b) = C(a-1, b-1) + C(a-1, b); it only needs additions. Take the modulo after every multiplication, and note the final - 1: if ways is 0 modulo the prime, the result must wrap around to 10^9 + 6, not become negative.
Complexity: O(n2) time and space: the triangle has n2 entries, and each level of the recursion splits its values in linear time.
class Solution:
def numOfWays(self, nums: List[int]) -> int:
mod = 10**9 + 7
n = len(nums)
# Pascal's triangle: comb[a][b] = C(a, b) mod p
comb = [[0] * n for _ in range(n)]
for a in range(n):
comb[a][0] = 1
for b in range(1, a + 1):
comb[a][b] = (comb[a - 1][b - 1] + comb[a - 1][b]) % mod
def ways(seq: List[int]) -> int:
"""Orders of seq (including seq itself) that build the same BST."""
if len(seq) < 3:
return 1
root = seq[0]
left = [x for x in seq if x < root]
right = [x for x in seq if x > root]
# choose which of the len(seq) - 1 slots after the root hold the left values
mix = comb[len(left) + len(right)][len(left)]
return mix * ways(left) % mod * ways(right) % mod
return (ways(nums) - 1) % mod # do not count the original order