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). ...
随机推荐
- CSS-06 外部JS,CSS文件的寻址问题
如果js.css外部文件有使用到相对路径,其相对路径的基准是不一样的 当一个index.html中引入外部的JS和CSS文件时: 在index.css文件中,相对路径的写法是以css文件相对于img图 ...
- unity DOTween Pro的使用--简化流程--自动播放
当gameobject setActive(true)的时候自动播放动画 1) 添加DoTween Animation. 设置动画效果, 略 选中 AutoPlay, 取消 AutoKill 2) 在 ...
- 基于Xilinx Zynq的计算处理平台
基于Xilinx Zynq XC7Z045 FFG 900的高性能计算模块 本模块基于Xilinx公司的FPGA XC7Z045 FFG 9000 芯片, 支持64bitDDR3, 容量2GByte: ...
- (ACM模板)不定长数组vector
#include<iostream> #include<cstdio> #include<vector> #include<algorithm> usi ...
- 洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom(Tarjan)
一道tarjan的模板水题 在这里还是着重解释一下tarjan的代码 #include<iostream> #include<cstdio> #include<algor ...
- Go的学习 append的使用
1. package main; import "fmt" func test () { ],,,,}; s:=a[:] fmt.Printf(]) s=append(s,); s ...
- phpstrom 安装
环境: ubuntu18.4 一. 安装 1. 下载安装包.tar.gz 下载地址:https://www.jetbrains.com/phpstorm/download/#section=linux ...
- hadoop通过java输出HAFS上的文件内容
package org.apache.hadoop.book;import java.io.InputStream;import java.net.URL;import org.apache.hado ...
- 51nod 1714:B君的游戏(博弈 sg打表)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1714 nim游戏的一个变形,需要打出sg函数的表 #incl ...
- 【Mybatis】Mybatis缓存
mybatis提供了缓存机制减轻数据库压力,提高数据库性能 mybatis的缓存分为两级:一级缓存.二级缓存 一级缓存是SqlSession级别的缓存,缓存的数据只在SqlSession内有效 二级缓 ...