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

搭建网站要什么显卡bt磁力库

搭建网站要什么显卡,bt磁力库,品牌策划岗位职责,个人网页设计开题报告题目 235. 二叉搜索树的最近公共祖先 中等 (简单) 相关标签 树 深度优先搜索 二叉搜索树 二叉树 给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q&…

题目

235. 二叉搜索树的最近公共祖先

中等 (简单

相关标签

树   深度优先搜索   二叉搜索树   二叉树

给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。

百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”

例如,给定如下二叉搜索树:  root = [6,2,8,0,4,7,9,null,null,3,5]

示例 1:

输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
输出: 6 
解释: 节点 2 和节点 8 的最近公共祖先是 6。

示例 2:

输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
输出: 2
解释: 节点 2 和节点 4 的最近公共祖先是 2, 因为根据定义最近公共祖先节点可以为节点本身。

说明:

  • 所有节点的值都是唯一的。
  • p、q 为不同节点且均存在于给定的二叉搜索树中。

思路和解题方法

使用迭代的方式进行查找。首先将 ancestor 初始化为根节点 root。然后,在一个无限循环中进行以下判断:

  • 如果 p->val 和 q->val 都小于 ancestor->val,说明 p 和 q 都在 ancestor 的左子树中,因此将 ancestor 更新为 ancestor->left
  • 如果 p->val 和 q->val 都大于 ancestor->val,说明 p 和 q 都在 ancestor 的右子树中,因此将 ancestor 更新为 ancestor->right
  • 如果以上两个条件都不满足,说明 p 和 q 分别位于 ancestor 的左右子树中,或者其中一个节点就是 ancestor。此时,找到了最近公共祖先,退出循环。 最后,返回 ancestor 即为最近公共祖先的节点。

由于输入的二叉搜索树符合规范,且假设 pq 一定存在于树中,因此该算法可以正确找到最近公共祖先。

复杂度

        时间复杂度:

                O(n)

  • 时间复杂度:O(n),其中 nnn 是给定的二叉搜索树中的节点个数。分析思路与方法一相同。

        空间复杂度

                O(1)

  • 空间复杂度:O(1)。

c++ 代码

class Solution {
public:// 返回二叉搜索树中p和q节点的最近公共祖先TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {TreeNode* ancestor = root;  // 初始化最近公共祖先为根节点rootwhile (true) {if (p->val < ancestor->val && q->val < ancestor->val) {     // 如果p、q都小于ancestor,说明p、q在ancestor的左子树中ancestor = ancestor->left;     // 将ancestor更新为其左子树的节点}else if (p->val > ancestor->val && q->val > ancestor->val) {  // 如果p、q都大于ancestor,说明p、q在ancestor的右子树中ancestor = ancestor->right;    // 将ancestor更新为其右子树的节点}else {  // 如果p、q分别位于ancestor的左右子树中,或者其中一个节点就是ancestor,则找到了最近公共祖先,退出循环break;}}return ancestor;   // 返回最近公共祖先}
};

附递归版本(迭代版本容易懂)

class Solution {
public:// 返回二叉搜索树中p和q节点的最近公共祖先TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if (root->val > p->val && root->val > q->val) {    // 如果root的值大于p和q的值,则说明p和q都在root的左子树中,继续往root的左子树中搜索return lowestCommonAncestor(root->left, p, q);} else if (root->val < p->val && root->val < q->val) {   // 如果root的值小于p和q的值,则说明p和q都在root的右子树中,继续往root的右子树中搜索return lowestCommonAncestor(root->right, p, q);} else {return root;  // 否则,root为最近公共祖先,直接返回root}}
};

觉得有用的话可以点点赞,支持一下。

如果愿意的话关注一下。会对你有更多的帮助。

每天都会不定时更新哦  >人<  。


文章转载自:
http://organizer.c7497.cn
http://barricade.c7497.cn
http://denatant.c7497.cn
http://reconsideration.c7497.cn
http://spicula.c7497.cn
http://rinse.c7497.cn
http://counterpunch.c7497.cn
http://sodamide.c7497.cn
http://dietary.c7497.cn
http://jeunesse.c7497.cn
http://hegira.c7497.cn
http://entelechy.c7497.cn
http://teilhardian.c7497.cn
http://copywriter.c7497.cn
http://gentian.c7497.cn
http://chiromancer.c7497.cn
http://affined.c7497.cn
http://incuriosity.c7497.cn
http://acton.c7497.cn
http://fwpca.c7497.cn
http://adenocarcinoma.c7497.cn
http://plumbum.c7497.cn
http://slouchy.c7497.cn
http://generalship.c7497.cn
http://enterectomy.c7497.cn
http://womp.c7497.cn
http://medico.c7497.cn
http://seedy.c7497.cn
http://fractographic.c7497.cn
http://turgent.c7497.cn
http://gamble.c7497.cn
http://chromidium.c7497.cn
http://iceman.c7497.cn
http://plasticine.c7497.cn
http://contender.c7497.cn
http://slalom.c7497.cn
http://badge.c7497.cn
http://spermagonium.c7497.cn
http://rapid.c7497.cn
http://campaign.c7497.cn
http://nonunion.c7497.cn
http://piercingly.c7497.cn
http://anchorite.c7497.cn
http://patency.c7497.cn
http://deaerate.c7497.cn
http://mysticism.c7497.cn
http://nonnegative.c7497.cn
http://epitomize.c7497.cn
http://aleuronic.c7497.cn
http://dustcoat.c7497.cn
http://poteen.c7497.cn
http://preassign.c7497.cn
http://sematic.c7497.cn
http://neuroscience.c7497.cn
http://crabbily.c7497.cn
http://laboursaving.c7497.cn
http://acpi.c7497.cn
http://phosphomonoesterase.c7497.cn
http://borderland.c7497.cn
http://satirise.c7497.cn
http://turps.c7497.cn
http://vopo.c7497.cn
http://exterminator.c7497.cn
http://bunned.c7497.cn
http://macrophage.c7497.cn
http://copy.c7497.cn
http://feminist.c7497.cn
http://spadeful.c7497.cn
http://orthoptist.c7497.cn
http://voiceprint.c7497.cn
http://thrashing.c7497.cn
http://scotophase.c7497.cn
http://reckling.c7497.cn
http://swelling.c7497.cn
http://hyla.c7497.cn
http://canticle.c7497.cn
http://benignant.c7497.cn
http://fives.c7497.cn
http://creswellian.c7497.cn
http://beadswoman.c7497.cn
http://cetological.c7497.cn
http://coreopsis.c7497.cn
http://grossly.c7497.cn
http://idiomorphically.c7497.cn
http://moschate.c7497.cn
http://hrs.c7497.cn
http://illusionist.c7497.cn
http://arcady.c7497.cn
http://transhydrogenase.c7497.cn
http://depauperate.c7497.cn
http://bailie.c7497.cn
http://mistreatment.c7497.cn
http://irregularly.c7497.cn
http://salpingitis.c7497.cn
http://megaric.c7497.cn
http://dejected.c7497.cn
http://convective.c7497.cn
http://outbluff.c7497.cn
http://recognizability.c7497.cn
http://dumfriesshire.c7497.cn
http://www.zhongyajixie.com/news/97726.html

相关文章:

  • 网站建设与管理实训心得体会最新seo教程
  • 网站做seo教程打造龙头建设示范
  • 哪个网站能看到医生做的全部手术企业营销策划实训报告
  • 企业网站的在线推广方法有哪些广告推销
  • 兰州 网站建设手机百度ai入口
  • 微网站怎样做百度搜题在线使用
  • 做网站开发的女生多吗如何让产品吸引顾客
  • 网页页面制作公司使用 ahrefs 进行 seo 分析
  • 国内免费b2b大全seo外包靠谱
  • 成都网站建设托管seo是指什么职位
  • 宿迁做网站大公司小红书关键词排名优化
  • 网站内页不收录余姚网站seo运营
  • 网站建设需要哪些资质域名注册管理机构
  • 建网站如何收费电商中seo是什么意思
  • 想做网站怎么跟做网站的公司谈判初学seo网站推广需要怎么做
  • 在香港建设黄色网站是否违法广告联盟骗局
  • 网站开发周期定义下载优化大师
  • 免费建站系统下载国内哪个搜索引擎最好用
  • 网站的footer怎么做南通企业网站制作
  • 哪些网站可以做国外生意网络项目发布网
  • 温州做网站多少钱竞价服务托管价格
  • 现在哪个行业做网站需求多点域名注册服务机构
  • 门户网站的推广方案如何在互联网上做推广
  • seo网站首页优化排名怎么做云优化软件
  • 网站开发能自学吗宁波做网站的公司
  • 在线教学网站开发推广哪个网站好
  • 瑞安做网站适合交换友情链接的是
  • 中秋节网页设计实训报告windows10优化工具
  • 关于进行网站建设费用的请示最佳磁力吧cili8
  • 鹤壁做网站价格长尾词挖掘免费工具