[leetcode 96]Unique Binary Searc
2018-07-25 本文已影响0人
安琪拉的小迷妹
题目链接
https://leetcode.com/problems/unique-binary-search-trees/description/

思路:求个数一般是动态规划
https://www.cnblogs.com/grandyang/p/4299608.html
https://blog.csdn.net/geekmanong/article/details/50442495
1. 由1,2,3,...,n构建的二叉查找树,以i为根节点,左子树由[1,i-1]构成,其右子树由[i+1,n]构成
2.就跟斐波那契数列一样,我们把n = 0 时赋为1,因为空树也算一种二叉搜索树,那么n = 1时的情况可以看做是其左子树个数乘以右子树的个数,左右字数都是空树,所以1乘1还是1

解释一下,当n=3时,当根为1时,左子树个数是0,右子树是[2,3],右子树n为2,所以是dp[2]
当根为2时,左子树是1,右子树是3,左子树是dp[1],右子树也是dp[1[
当根为3时,左子树是[1,2],右子树是[0],所以左子树是dp[2],右子树是dp[0]
