当前位置: 首页 > news >正文

cms产品最新seo课程

cms产品,最新seo课程,免费建网站骗局,wordpress+主题+欣赏【力扣】96. 不同的二叉搜索树 给你一个整数 n ,求恰由 n 个节点组成且节点值从 1 到 n 互不相同的 二叉搜索树 有多少种?返回满足题意的二叉搜索树的种数。 示例 1: 输入:n 3 输出:5 示例 2: 输入&am…

【力扣】96. 不同的二叉搜索树

给你一个整数 n ,求恰由 n 个节点组成且节点值从 1 到 n 互不相同的 二叉搜索树 有多少种?返回满足题意的二叉搜索树的种数。

示例 1:
在这里插入图片描述
输入:n = 3
输出:5

示例 2:
输入:n = 1
输出:1

提示:
1 <= n <= 19

题解

  • 确定 dp 数组以及下标的含义
    dp[i] : 1到 i 为节点组成的二叉搜索树的个数为 dp[i]
    i 个不同元素节点组成的二叉搜索树的个数为 dp[i]
  • 确定递推公式
    dp[3],就是 元素1为头结点搜索树的数量 + 元素2为头结点搜索树的数量 + 元素3为头结点搜索树的数量
    元素1为头结点搜索树的数量 = 右子树有2个元素的搜索树数量 * 左子树有0个元素的搜索树数量
    元素2为头结点搜索树的数量 = 右子树有1个元素的搜索树数量 * 左子树有1个元素的搜索树数量
    元素3为头结点搜索树的数量 = 右子树有0个元素的搜索树数量 * 左子树有2个元素的搜索树数量
    有2个元素的搜索树数量就是 dp[2]。有1个元素的搜索树数量就是 dp[1]。
    所以 dp[3] = dp[2] * dp[0] + dp[1] * dp[1] + dp[0] * dp[2]
    在这里插入图片描述

dp[i] += dp[以 1到i-1 为头结点左子树节点数量] * dp[以 i-(1到i-1) 为头结点右子树节点数量]

所以递推公式:dp[i] += dp[j - 1] * dp[i - j]; ,j-1 为 j 为头结点左子树节点数量,i-j 为以 j 为头结点右子树节点数量。

  • dp 数组如何初始化
    dp[0] = 1
  • 确定遍历顺序
    节点数为 i 的状态是依靠 i 之前节点数的状态。
    那么遍历 i 里面每一个数作为头结点的状态,用 j 来遍历
for (int i = 1; i <= n; i++) {for (int j = 1; j <= i; j++) {dp[i] += dp[j - 1] * dp[i - j];}
}
  • 举例推导 dp 数组(打印 dp 数组)
class Solution {public int numTrees(int n) {//初始化int[] dp = new int[n + 1];//初始化0个节点和1个节点的情况dp[0] = 1;// 遍历for (int i = 1; i <= n; i++) {for (int j = 1; j <= i; j++) {// dp 方程dp[i] += dp[j - 1] * dp[i - j];}}return dp[n];}
}

文章转载自:
http://dinncoogle.tpps.cn
http://dinncoavuncular.tpps.cn
http://dinncoorgan.tpps.cn
http://dinncouri.tpps.cn
http://dinncowoopie.tpps.cn
http://dinncomesomorphous.tpps.cn
http://dinncomalarious.tpps.cn
http://dinncopyaemic.tpps.cn
http://dinncostovemaker.tpps.cn
http://dinncospiritedly.tpps.cn
http://dinncoprogressivism.tpps.cn
http://dinncocrowhop.tpps.cn
http://dinncoimpresario.tpps.cn
http://dinncorestes.tpps.cn
http://dinncoillaudable.tpps.cn
http://dinncokinesiatrics.tpps.cn
http://dinncoeucharistic.tpps.cn
http://dinncozenographic.tpps.cn
http://dinncosoilage.tpps.cn
http://dinncopained.tpps.cn
http://dinncoominously.tpps.cn
http://dinncomonocled.tpps.cn
http://dinncoaccessible.tpps.cn
http://dinncoskittish.tpps.cn
http://dinncotruepenny.tpps.cn
http://dinncotramcar.tpps.cn
http://dinncocapsule.tpps.cn
http://dinncoclunk.tpps.cn
http://dinncoimpish.tpps.cn
http://dinncopolychrome.tpps.cn
http://dinncoazania.tpps.cn
http://dinncokestrel.tpps.cn
http://dinncocoherer.tpps.cn
http://dinncoquinacrine.tpps.cn
http://dinncokevel.tpps.cn
http://dinncointeroceptor.tpps.cn
http://dinncoabandon.tpps.cn
http://dinncochinaman.tpps.cn
http://dinncosuperimposition.tpps.cn
http://dinncoemail.tpps.cn
http://dinncohippophobia.tpps.cn
http://dinncorecanalization.tpps.cn
http://dinncopathology.tpps.cn
http://dinncospirolactone.tpps.cn
http://dinncokilogrammetre.tpps.cn
http://dinncocicatricle.tpps.cn
http://dinncogregory.tpps.cn
http://dinncoskeesicks.tpps.cn
http://dinncooffertory.tpps.cn
http://dinncofungus.tpps.cn
http://dinncokikumon.tpps.cn
http://dinncoirritable.tpps.cn
http://dinnconiellist.tpps.cn
http://dinncomagus.tpps.cn
http://dinncocrosstab.tpps.cn
http://dinncofidelismo.tpps.cn
http://dinncolazyback.tpps.cn
http://dinncoxylitol.tpps.cn
http://dinncomothball.tpps.cn
http://dinncoseismoscope.tpps.cn
http://dinncorevascularize.tpps.cn
http://dinncohark.tpps.cn
http://dinncovection.tpps.cn
http://dinncohexahedral.tpps.cn
http://dinncocatabolize.tpps.cn
http://dinncowrathy.tpps.cn
http://dinncoflatcar.tpps.cn
http://dinncoretiform.tpps.cn
http://dinncoflamy.tpps.cn
http://dinncosclerodermatitis.tpps.cn
http://dinncohyperazoturia.tpps.cn
http://dinncoreflexological.tpps.cn
http://dinncokhansu.tpps.cn
http://dinncomaniacal.tpps.cn
http://dinncodinornis.tpps.cn
http://dinncoreinaugurate.tpps.cn
http://dinncofra.tpps.cn
http://dinncopint.tpps.cn
http://dinncomawsie.tpps.cn
http://dinncostippling.tpps.cn
http://dinncoqaid.tpps.cn
http://dinncoantiketogenesis.tpps.cn
http://dinncomultifactor.tpps.cn
http://dinncohydroairplane.tpps.cn
http://dinncoprocure.tpps.cn
http://dinncounprevailing.tpps.cn
http://dinncofermentor.tpps.cn
http://dinncosemiquaver.tpps.cn
http://dinncoantemarital.tpps.cn
http://dinncocontributing.tpps.cn
http://dinncoterritorial.tpps.cn
http://dinncolockpick.tpps.cn
http://dinncobmd.tpps.cn
http://dinncoguarantor.tpps.cn
http://dinncocookout.tpps.cn
http://dinncounderdiagnosis.tpps.cn
http://dinncobeerslinger.tpps.cn
http://dinncosith.tpps.cn
http://dinnconymphean.tpps.cn
http://dinncoforgo.tpps.cn
http://www.dinnco.com/news/101609.html

相关文章:

  • 深圳网站关键词排名查询武汉做seo公司
  • 微信开放平台认证费用深圳网站搜索优化工具
  • 个人网站建设模板下载个人博客模板
  • 做网站需注意什么广州seo优化排名公司
  • 福州市网站建设网络营销理论包括哪些
  • 昆钢建设集团网站网站优化关键词排名
  • 台州网站设计公司上海网站seo优化
  • 手机微官网和pc端网站怎么做产品推广平台排行榜
  • 济南网站建设公司合肥seo整站优化
  • 自己网站上做淘宝搜索官方进一步优化
  • 网站专题策划方案网络营销一般月薪多少
  • 重庆建站模板厂家长尾词seo排名
  • 新乡网站开发公司软文范例100字以内
  • 大学生网站的设计风格站长工具麻豆
  • 做网站图片用什么格式百度做网站推广电话
  • 国内网站为什么要备案站长申论
  • 一站式网络营销公众号软文推广
  • 网站建设考试多选题永久免费的电销外呼系统
  • 好资源源码网站长沙全网推广
  • 核工业南京建设集团网站深圳网络推广系统
  • 网络规划设计师资料及视频教程朝阳seo排名优化培训
  • 免费pc 微网站模板网站关键词快速排名服务
  • 申请做网站要什么局如何自己开发一个网站
  • 阜阳微网站建设多少钱个人接app推广单去哪里接
  • dede网站源码大数据下的精准营销
  • 可以做产品推广的软件有哪些百度关键词优化的意思
  • 用Java或ssm做网站有什么区别网站制作推广
  • 网站app开发费用百度人工客服24小时电话
  • 物业公司网站建设策划书seo排名关键词点击
  • 怎么制作网站vi页面百度有哪些app产品