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 reordernumssuch that the BST formed is identical to the original BST formed fromnums.
Since the answer may be very large, return it modulo10^9 + 7.
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:
1
2
3
4
5
6
7
8
9
10
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:
1
2
3
4
5
Input:
nums = [1,2,3]
Output:
0
Explanation: There are no other orderings of nums that will yield the same BST.
The number of ways to reorder the array to get the same BST is determined by the number of ways to interleave left and right subtrees, which is a combinatorial problem.
classSolution {
int MOD =1e9+7;
vector<vector<int>> comb;
intdfs(vector<int>& nums) {
int n = nums.size();
if (n <=2) return1;
vector<int> left, right;
for (int i =1; i < n; ++i) {
if (nums[i] < nums[0]) left.push_back(nums[i]);
else right.push_back(nums[i]);
}
int l = dfs(left), r = dfs(right);
return (longlong)comb[n-1][left.size()] * l % MOD * r % MOD;
}
public:int numOfWays(vector<int>& nums) {
int n = nums.size();
comb.assign(n+1, vector<int>(n+1, 0));
for (int i =0; i <= n; ++i) {
comb[i][0] = comb[i][i] =1;
for (int j =1; j < i; ++j)
comb[i][j] = (comb[i-1][j-1] + comb[i-1][j]) % MOD;
}
return (dfs(nums) -1+ MOD) % MOD;
}
};
classSolution {
int MOD = 1_000_000_007;
int[][] comb;
publicintnumOfWays(int[] nums) {
int n = nums.length;
comb =newint[n+1][n+1];
for (int i = 0; i <= n; ++i) {
comb[i][0]= comb[i][i]= 1;
for (int j = 1; j < i; ++j)
comb[i][j]= (comb[i-1][j-1]+ comb[i-1][j]) % MOD;
}
return (dfs(nums) - 1 + MOD) % MOD;
}
privateintdfs(int[] nums) {
int n = nums.length;
if (n <= 2) return 1;
List<Integer> left =new ArrayList<>(), right =new ArrayList<>();
for (int i = 1; i < n; ++i) {
if (nums[i]< nums[0]) left.add(nums[i]);
else right.add(nums[i]);
}
int l = dfs(left.stream().mapToInt(i->i).toArray());
int r = dfs(right.stream().mapToInt(i->i).toArray());
return (int)((long)comb[n-1][left.size()]* l % MOD * r % MOD);
}
}
classSolution {
val MOD = 1_000_000_007
lateinitvar comb: Array<IntArray>
funnumOfWays(nums: IntArray): Int {
val n = nums.size
comb = Array(n+1) { IntArray(n+1) }
for (i in0..n) {
comb[i][0] = 1; comb[i][i] = 1for (j in1 until i)
comb[i][j] = (comb[i-1][j-1] + comb[i-1][j]) % MOD
}
return (dfs(nums) - 1 + MOD) % MOD
}
fundfs(nums: IntArray): Int {
val n = nums.size
if (n <=2) return1val left = mutableListOf<Int>()
val right = mutableListOf<Int>()
for (i in1 until n) {
if (nums[i] < nums[0]) left.add(nums[i]) else right.add(nums[i])
}
val l = dfs(left.toIntArray())
val r = dfs(right.toIntArray())
return (comb[n-1][left.size] * l.toLong() % MOD * r % MOD).toInt()
}
}
1
2
3
4
5
6
7
8
9
10
classSolution:
defnumOfWays(self, nums: list[int]) -> int:
from math import comb as C
MOD =10**9+7defdfs(arr):
if len(arr) <=2: return1 left = [x for x in arr[1:] if x < arr[0]]
right = [x for x in arr[1:] if x > arr[0]]
return C(len(arr)-1, len(left)) * dfs(left) % MOD * dfs(right) % MOD
return (dfs(nums) -1) % MOD