40.Unique Binary Search Trees(不同的二叉搜索树)
Level:
Medium
题目描述:
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n?
Example:
Input: 3
Output: 5
Explanation:
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
思路分析:
动态规划的思想,二叉树有个性质,就是左子树的节点值都比根小,右子树的节点值都比根大,题目说明二叉树的节点值是从1到n,所以我们能够确定如果根为k,那么左子树的值是1到k-1,右子树的值是k+1到n。还有一点是,对于一个根来说,唯一二叉树的数量是其左子树的数量乘上右子树的数量,这是简单的乘法原理。并且左右子树的形态数量跟具体的数没有关系,只跟这个树里有多少个节点有关。而根可以选择从1到n的任意数,唯一二叉树的总数,就是根为1到n的树相加。所以该问题化简为以k为根,其唯一左子树和右子树各有多少,这就是动态规划问题了,我们建立一个数组dp[ i ],代表节点数为i的唯一子树有多少个。显然dp[0]=dp[1]=1。
代码:
public class Solution{
public int numTrees(int n){
int []dp=new int[n+1]; //dp[i]代表的是节点数为i的唯一子树有多少个
dp[0]=dp[1]=1;
for(int i=2;i<=n;i++){//表示一共有多少的节点
for(int j=0;j<i;j++){ //表示左子树有多少节点
dp[i]=dp[i]+dp[j]*dp[i-j-1];
}
}
return dp[n];
}
}
40.Unique Binary Search Trees(不同的二叉搜索树)的更多相关文章
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [LeetCode] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- [LeetCode] 95. Unique Binary Search Trees II 唯一二叉搜索树 II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [Leetcode] Unique binary search trees ii 唯一二叉搜索树
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- LeetCode OJ:Unique Binary Search Trees(唯一二叉搜索树)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- Leetcode96.Unique Binary Search Trees不同的二叉搜索树
给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 示例: 输入: 3 输出: 5 解释: 给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 假设n个节点存在二叉排序树的 ...
- 【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】
[096-Unique Binary Search Trees(唯一二叉搜索树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given n, how many s ...
- LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索树的众数)
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...
- LeetCode第[98]题(Java):Validate Binary Search Tree(验证二叉搜索树)
题目:验证二叉搜索树 难度:Medium 题目内容: Given a binary tree, determine if it is a valid binary search tree (BST). ...
随机推荐
- canvas 画正方形和圆形
绘制正方形 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- redis的hmset乐观锁的实现
1.lua脚本(集成实现了乐观锁,hmset ,expire等) local key=KEYS[1]; local oldVerion=tonumber(ARGV[1]); local seconds ...
- 使用eclipse创建Spring Boot项目
环境介绍 1.jdk1.8 2.eclipse 3.maven 3.5.0 创建项目 eclectic 左上角 file -> new -> maven project 出现下图默认就好, ...
- 《深入学习Redis(1):Redis内存模型 》笔记,待完善
参考资料 https://www.cnblogs.com/kismetv/p/8654978.html 一.内存统计 info memory 查看内存统计 五.应用举例
- 三、Angular项目,app.module.ts解析
1. 项目主要文件存放的路径 2.app.module.ts模块解析 3.模块和组件关系 |--app.module.ts(模块) |--app.component.ts(组件) |--app.co ...
- 注解@requestBody自动封装复杂对象 (成功,自己的例子封装的不是一个复杂对象,只是一个简单的User对象,将jsp页面的name转成json字符串,再用JSON.stringify()传参就行了)
注意:ajax向后台传值的时候,必须加上contentType:"application/json"; springmvc的注解@requestBody可以通过页面提交json来自 ...
- k8s和docker区别
简要介绍: docker是一个开源的应用容器引擎,开发者可以打包他们的应用以及依赖到一个容器中,发布到流行的liunx系统上,或者实现虚拟化. k8s是一个开源的容器集群管理系统,可以实现容器集群的自 ...
- java连接远程服务器并执行命令
导入必要的jar包 <dependency> <groupId>ch.ethz.ganymed</groupId> <artifactId>ganym ...
- boost asio scalability and multithreading
A library such as Boost.Asio is typically used to achieve greater efficiency. With no need to wait f ...
- webpackES6语法转ES5语法
安装babel-loader: npm install --save-dev babel-loader@7 babel-core babel-preset-es2015 webpack.config. ...