96. Unique Binary Search Trees

Given an integer n, return the number of structurally unique BST's (binary search trees) which has exactly n nodes of unique values from 1 to n.

Example 1:

Input: n = 3
Output: 5

Example 2:

Input: n = 1
Output: 1

Constraints:

  • 1 <= n <= 19
    class Solution {
    public:
        int numTrees(int n) {
            int dp[n + 10];
            memset(dp,0,sizeof(dp));
            dp[0] = 1;
            for(int i = 1;i <= n;i ++)
                for(int j = 1;j <= i;j ++){
                    dp[i] += dp[j - 1] * dp[i-j];
                }
            return dp[n];
    
        }
    };

相关推荐

  1. 小白月赛96

    2024-03-17 01:32:02       11 阅读
  2. 96 双指针解旋转链表

    2024-03-17 01:32:02       40 阅读
  3. 力扣labuladong——一刷day96

    2024-03-17 01:32:02       35 阅读
  4. 面试经典150题(90-92)

    2024-03-17 01:32:02       41 阅读
  5. 面试经典150题(93-95)

    2024-03-17 01:32:02       37 阅读
  6. 面试经典150题(96-100)

    2024-03-17 01:32:02       32 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-17 01:32:02       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-17 01:32:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-17 01:32:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-17 01:32:02       20 阅读

热门阅读

  1. linux下自定义显示文件拷贝进度

    2024-03-17 01:32:02       24 阅读
  2. 2024/3/26

    2024/3/26

    2024-03-17 01:32:02      19 阅读