Python/C++用組合數學解難題Leetcode 1569重新排列陣列獲得相同二元搜尋樹之總數 Number of Ways to Reorder Array to Get Same BST

Опубликовано: 10 Апрель 2026
на канале: An-Wen Deng
211
like

Python/C++用組合數學解難題#Leetcode 1569重新排列陣列獲得相同二元搜尋樹之總數 Number of Ways to Reorder Array to Get Same BST
The first element in array must be the root. Then divide the array into left subtree and right subtree!
Use recursion, if the subproblems for left subtree and right subtree are solved, with the returning number l and r, Use the following formula:
TotalNumber=l*r*C_ Len(left_subtree)^(N−1)−1
to solve the problem! Remember modulo 10**9+7

[ Code on LeetCode]https://leetcode.com/problems/number-...
[LeetCode程式]   • C語言版的井字遊戲 TicTacTao(上)  
=============
Python/C++使用組合數學解決問題之方法:重新排列陣列獲得相同二元搜尋樹的方法數 首先,陣列的第一個元素必須成為樹的根節點。
接著將陣列分成左子樹和右子樹。 利用遞迴,如果左子樹和右子樹的子問題已經解決,分別返回數字 l 和 r,則可以使用以下公式解決問題:
總方法數 = l * r * C_左子樹長度^(N-1) -1
記得要對計算結果進行模 10**9+7 的運算。