
96. Unique Binary Search Trees
发布日期:2021-05-09 02:50:43
浏览次数:21
分类:博客文章
本文共 885 字,大约阅读时间需要 2 分钟。
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n?
Example:
Input: 3Output: 5Explanation: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
Approach #1: DP. [C++]
class Solution {public: int numTrees(int n) { vector nums(n+1); nums[0] = 1; nums[1] = 1; for (int i = 2; i <= n; ++i) { for (int j = 0; j <= i-1; ++j) { nums[i] += nums[j] * nums[i-1-j]; } } return nums[n]; }};
Analysis:
status: nums[n] represent that there have n nodes the result of BST's number.
init: nums[1] = 1, nums[0] = 1;
function: ∑(nums[j] * nums[i-1-j]);
result: nums[n]
发表评论
最新留言
表示我来过!
[***.240.166.169]2025年04月11日 23时55分38秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Consul安装使用
2021-05-09
手动造轮子——基于.NetCore的RPC框架DotNetCoreRpc
2021-05-09
Python IO编程
2021-05-09
CSS入门总结
2021-05-09
使用 TortoiseGit 时,报 Access denied 错误
2021-05-09
基于 HTML5 WebGL 的污水处理厂泵站自控系统
2021-05-09
[系列] Go gRPC 调试工具
2021-05-09
django-表单之模型表单渲染(六)
2021-05-09
c++之程序流程控制
2021-05-09
一位年轻而优秀的.NET开发者的成长点滴
2021-05-09
如何使用ABP进行软件开发(1) 基础概览
2021-05-09
Spark学习之SparkStreaming
2021-05-09
AlwaysOn配置时在连接步骤时报错(35250)
2021-05-09
排序算法之总结
2021-05-09
微软云Linux服务器 Mysql、tomcat远程连接错误解决办法
2021-05-09
Python数据分析(二): Numpy技巧 (2/4)
2021-05-09
09 . Python3之常用模块
2021-05-09
Python学习笔记-StatsModels 统计回归(3)模型数据的准备
2021-05-09
Velocity.js初步
2021-05-09