/*
这道题的关键是:动态表尽量的选取,知道二叉搜索树中左子树的点都比根节点小,右子树的点都比根节点大
所以当i为根节点,左子树有i-1个点,右子树有n-i个点,左右子树就可以开始递归构建,过程和一开始的过程是一样的,
而左右子树的特征中可以知道的就是点的个数,所以用点的个数作为动态变量就很好。
这样就给我们提供了一个思路:动态规划和树结合的题,由于子问题就是左右树,所以动态变量的选取基本就是考虑题目给出的信息
中,可以从根节点得到左右子树的哪些特征,用这个特征作为动态变量。 二叉搜索树中,都是左子树的点小于根节点,右子树的点大于根节点,对于所有子树都是这样。
用dp[i]表示有i个数的时候能组成多少树 dp[i] = dp[0]*dp[i-1]+ ...+ dp[i-1]*dp[0]
等号右边分别代表1到i分别是根节点的情况
*/
int[] dp = new int[n+1];
dp[0] = 1;//当没有节点时,就只有一种情况
for (int i = 1; i < n+1; i++) {
for (int j = 0; j < i; j++) {
dp[i] += dp[j] * dp[i-j-1];
}
}
return dp[n];
 1  public List<TreeNode> generateTrees(int n) {
2 /*
3 从i到n依次选取做为根节点,生成一棵树.根节点为i的这个过程我们叫做function(i),则function(i)中左子树就是从1到i-1的
4 生成过程,右子树就是从i+1到n的过程,递归function就行,然后对左右子树就行全匹配就行
5 */
6 if (n < 1)
7 return new ArrayList<TreeNode>();
8 return build(1, n);
9 }
10
11 public List<TreeNode> build(int start, int end) {
12 List<TreeNode> res = new ArrayList<>();
13 if (start > end)
14 {
15 res.add(null);
16 return res;
17 }
18 for (int i = start; i <= end; i++) {
19 List<TreeNode> left = build(start, i - 1);
20 List<TreeNode> right = build(i + 1, end);
21
22 for (TreeNode lt :
23 left) {
24 for (TreeNode rt :
25 right) {
26 //由于每次组合都形成一个新的树,所以新建树应该是在这里
27 TreeNode root = new TreeNode(i);
28 root.left = lt;
29 root.right = rt;
30 //注意添加的位置,每组合一个左右子树就会形成一种情况
31 res.add(root);
32 }
33 }
34
35 }
36 return res;
37 }

96. Unique Binary Search Trees1和2的更多相关文章

  1. 52. leetcode 96. Unique Binary Search Trees

    96. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) tha ...

  2. [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  3. leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses

    96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...

  4. 96. Unique Binary Search Trees

    题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For e ...

  5. 【一天一道LeetCode】#96. Unique Binary Search Trees

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...

  6. [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆

    [Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...

  7. 96. Unique Binary Search Trees (Tree; DP)

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  8. 【LeetCode】96. Unique Binary Search Trees (2 solutions)

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  9. 96. Unique Binary Search Trees(I 和 II)

    Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For example ...

随机推荐

  1. jdk从1.8降到jdk1.7失败

    1.将JAVA_HOME:的路径更改为1.7的相关路径,例如我的:C:\Java\jdk1.7.0_80 2.此时查看版本:Java -version,如果是1.8的版本,就把path路径下的%JAV ...

  2. 20191225_关于sql中exists和not exists

    exists n. 存在量词(exist的复数)v. 存在:出现:活着(exist的三单形式) 理所当然 not exists 就是不存在 那么 if  exists 就是表示它引导的子句有结果集返回 ...

  3. sql server full join拼接表数据,按组加序号

    --查询所有数据select A.*,B.* from(select z.id,z.requestId,z.FBillNo,dt5.FCauseAndProcess,dt5.FEquipmentNo, ...

  4. moviepy1.03音视频剪辑:使用manual_tracking和headblur实现追踪人脸打马赛克

    ☞ ░ 前往老猿Python博文目录 ░ 一.引言 在moviepy官网的案例<Tracking and blurring someone's face>和CSDN的moviepy大神uc ...

  5. Python文件学习遇到的问题

    关于open函数文件打开模式的有意思的一个现象 关于Python中中文文本文件使用二进制方式读取后的解码UnicodeDecodeError问题 Python中str类型的字符串写入二进制文件时报Ty ...

  6. 3、pytorch实现最基础的MLP网络

    %matplotlib inline import numpy as np import torch from torch import nn import matplotlib.pyplot as ...

  7. 百度前端技术学院-基础-day25-27

    倒数开始 滴答滴 滴答滴 task1 题目: 我们现在来做一个最简单的时钟,通过小练习来学习 Date,复习定时,然后再练习一下函数的封装具体需求如下: 在页面中显示当前日期及时间,按秒更新 格式为 ...

  8. bootstrap table 嵌入百分比进度条

  9. uni-app快速入门教程

    1.什么是uni-app? uni-app 是一个使用 Vue.js 开发所有前端应用的框架,开发者编写一套代码,可发布到iOS.Android.H5.以及各种小程序(微信/支付宝/百度/头条/QQ/ ...

  10. Android 11 源码下载+编译教程

    下载AOSP源码 这里我使用的是外国语大学的镜像执行的下载,Mac系统的话,一定要在区分大小姐的磁盘下执行 repo init -u https://mirrors.bfsu.edu.cn/git/A ...