【leetcode】Unique Binary Search Trees

Given n, how many structurally unique BST‘s (binary search trees) that store values 1...n?

For example,
Given n = 3, there are a total of 5 unique BST‘s.

注意:二分查找树的定义是,左子树节点均小于root,右子树节点均大于root!

不要想当然地将某个点作为root时,认为其他所有节点都能全部放在left/right中,除非这个点是 min 或者 max 的。

   1         3     3      2      1
    \       /     /      / \           3     2     1      1   3      2
    /     /       \                    2     1         2                 3

分析思路:

n个点中每个点都可以作为root,

当 i 作为root时,小于 i  的点都只能放在其左子树中,大于 i 的点只能放在右子树中,

此时只需求出左、右子树各有多少种,二者相乘即为以 i 作为root时BST的总数。

解题技巧:

求数量,要首先想到使用动态规划(dp)

如果不只是求数量,还要把所有的树都枚举出来,就要使用dfs(深度优先搜索)来遍历决策树了。

这道题是使用动态规划来解决的。

那么如何去求这个问题的状态转移方程呢?其实大部分动态规划的难点都是求状态转移方程。

n=0时,为空树,那么dp[0]=1;

n=1时,显然也是1,dp[1]=1;

n=2时,dp[2]=2;

对于n>2时,dp[n]=dp[0]*dp[n-1]+dp[1]*dp[n-2]+......+dp[n-1]*dp[0]

典型的动态规划题目,因为有重叠的子问题,当前决策依赖于子问题的解。

同时,当根节点元素为 1, 2, 3, 4, 5, ..., i, ..., n时,基于以下原则的BST树具有唯一性: 
以i为根节点时,

其左子树构成为[0,...,i-1],

其右子树构成为[i+1,...,n]构成。

Python:

 1 class Solution:
 2     def numTrees(self,n):
 3         dp=[1,1,2]
 4         if n<=2:
 5             return dp[n]
 6         else:
 7             dp+= [0 for i in range(n-2)]
 8             for i in range(3,n+1):
 9                 for j in range(1,i+1):
10                     dp[i]+=dp[j-1]*dp[i-j]
11             return dp[n]

C++:

 1 class Solution {
 2 public:
 3     int numTrees(int n) {
 4       vector<int> num;
 5       num.push_back(1);
 6       for(int i=1;i<=n;i++)
 7       {
 8           num.push_back(0);
 9           if(i<3)
10             num[i]=i;
11           else
12           {
13               for(int j=1;j<=i;j++)
14                 num[i]+=num[j-1]*num[i-j];
15           }
16       }
17
18       return num[n];
19     }
20 };

这道题的模型正好是卡特兰数的定义。

可以用卡特兰数的通项公式来求解,这样时间复杂度就可以降低到O(n)。

在其他文章对 卡特兰数 进行稍微浅显的认识。



时间: 2024-08-10 19:17:42

【leetcode】Unique Binary Search Trees的相关文章

【LeetCode】Unique Binary Search Trees II 不同的二叉查找树II

今早起来做 LeetCode,结果被这道题卡了将近1个半小时,忍着没有去搜答案,最后还是被我想出来了,而且直接一次AC,哈哈!现在在这里记录一下解题思路. 原题: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's sh

【LeetCode】Unique Binary Search Trees (2 solutions)

Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 Base case: n==0, n==1时,f

【Leetcode】Unique Binary Search Trees II

题目链接:https://leetcode.com/problems/unique-binary-search-trees-ii/ 题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below.

【leetcode】Unique Binary Search Trees (#96)

Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, given n = 3, there are a total of 5 unique BST's. 1         3     3      2      1     \       /     /      / \      \      3     2     1      1  

【leetcode】 Unique Binary Search Trees II (middle)☆

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 这次的题目要求是得到所有的树. 我的思路: 用f[

【leetcode】 Unique Binary Search Trees (middle)☆

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. 找数字连续最大乘积子序列. 思路:这个麻烦在有负数和0,我的方法,如果有0,一切都设

【树】Unique Binary Search Trees II

题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 思路: 找到一个数作为根结点,剩余的数分别

【树】Unique Binary Search Trees

题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 思路: 依次把每个节点作为根节点,左边节点作为左子树,右边节点作为右子树,那么总的数目等于左子

【leetcode刷题笔记】Unique Binary Search Trees II

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 题解:递归的枚举1~n的每个节点为根节点,然后递归