dfs(nums)
that returns the number of valid permutations for a given subtree.nums
has less than 3 elements, return 1 as there is only one way to form the BST.nums
as the root and partition the rest into left_nodes
and right_nodes
based on their values relative to the root.dfs
on left_nodes
and right_nodes
to get the number of valid permutations for each subtree.C(left+right, left)
to determine the number of ways to interleave the two subtrees.nums
.dfs(nums)
modulo 10^9 + 7
.