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). ...
随机推荐
- eclipse 设置注释模板
window->preference->java->code styple->code template->Comments Types /** * @author $ ...
- 2018-2-13-C#-复制列表
title author date CreateTime categories C# 复制列表 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:23:3 +0 ...
- CSS实现三级菜单[转]
头部导航条布局 html代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"&g ...
- canvas 星星闪烁的效果
代码实例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- html/css 图片展示效果
demo.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- Python3.5-20190521-廖老师-自我笔记-单元测试
执行结果
- Es学习第八课, Filter、bool和范围查询
Filter过滤查询 filter是不计算相关性的,同时可以缓存.因此filter速度快于query. 我们先在kibana上先添加数据来做准备 POST /lib4/items/_bulk { &q ...
- 【leetcode】1032. Stream of Characters
题目如下: Implement the StreamChecker class as follows: StreamChecker(words): Constructor, init the data ...
- 【leetcode】1014. Capacity To Ship Packages Within D Days
题目如下: A conveyor belt has packages that must be shipped from one port to another within D days. The ...
- springMVC使用map接收入参 + mybatis使用map 传入查询参数
测试例子: controllel层 ,使用map接收请求参数,通过Debug可以看到,请求中的参数的值都是字符串形式,如果将这个接收参数的map直接传入service,mybatis接收参数时会报错, ...