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

网站开发需要文章写的好吗怎么建立网站卖东西

网站开发需要文章写的好吗,怎么建立网站卖东西,南宁网站平台,中国十大装修公司排名文章目录 题目标题和出处难度题目描述要求示例数据范围 解法思路和算法代码复杂度分析 题目 标题和出处 标题:完全二叉树插入器 出处:919. 完全二叉树插入器 难度 6 级 题目描述 要求 完全二叉树是每一层(除最后一层外)都…

文章目录

  • 题目
    • 标题和出处
    • 难度
    • 题目描述
      • 要求
      • 示例
      • 数据范围
  • 解法
    • 思路和算法
    • 代码
    • 复杂度分析

题目

标题和出处

标题:完全二叉树插入器

出处:919. 完全二叉树插入器

难度

6 级

题目描述

要求

完全二叉树是每一层(除最后一层外)都是完全填充的,并且所有的结点都尽可能地集中在左侧。

设计一种算法,将一个新结点插入到一个完全二叉树中,并在插入后保持完全二叉树。

实现 CBTInserter \texttt{CBTInserter} CBTInserter 类:

  • CBTInserter(TreeNode root) \texttt{CBTInserter(TreeNode root)} CBTInserter(TreeNode root) 使用完全二叉树的根结点 root \texttt{root} root 初始化数据结构;
  • int insert(int v) \texttt{int insert(int v)} int insert(int v) 向树中插入一个值为 val \texttt{val} val 的新结点,使树保持完全二叉树的状态,并返回插入结点的父结点的值;
  • TreeNode get_root() \texttt{TreeNode get\_root()} TreeNode get_root() 返回树的头结点。

示例

示例 1:

示例 1

输入:
["CBTInserter", "insert", "insert", "get_root"] \texttt{["CBTInserter", "insert", "insert", "get\_root"]} ["CBTInserter", "insert", "insert", "get_root"]
[[[1, 2]], [3], [4], []] \texttt{[[[1, 2]], [3], [4], []]} [[[1, 2]], [3], [4], []]
输出:
[null, 1, 2, [1, 2, 3, 4]] \texttt{[null, 1, 2, [1, 2, 3, 4]]} [null, 1, 2, [1, 2, 3, 4]]
解释:
CBTInserter cBTInserter = new CBTInserter([1, 2]); \texttt{CBTInserter cBTInserter = new CBTInserter([1, 2]);} CBTInserter cBTInserter = new CBTInserter([1, 2]);
cBTInserter.insert(3); \texttt{cBTInserter.insert(3);} cBTInserter.insert(3); // 返回 1 \texttt{1} 1
cBTInserter.insert(4); \texttt{cBTInserter.insert(4);} cBTInserter.insert(4); // 返回 2 \texttt{2} 2
cBTInserter.get_root(); \texttt{cBTInserter.get\_root();} cBTInserter.get_root(); // 返回 [1, 2, 3, 4] \texttt{[1, 2, 3, 4]} [1, 2, 3, 4]

数据范围

  • 树中结点数目在范围 [1, 1000] \texttt{[1, 1000]} [1, 1000]
  • 0 ≤ Node.val ≤ 5000 \texttt{0} \le \texttt{Node.val} \le \texttt{5000} 0Node.val5000
  • root \texttt{root} root 是完全二叉树
  • 0 ≤ val ≤ 5000 \texttt{0} \le \texttt{val} \le \texttt{5000} 0val5000
  • 最多调用 10 4 \texttt{10}^\texttt{4} 104 insert \texttt{insert} insert get_root \texttt{get\_root} get_root

解法

思路和算法

这道题的解法不唯一。一种常规的解法是维护完全二叉树中的结点数,每次插入结点时根据结点数定位到插入结点的位置。当完全二叉树中有 n n n 个结点时,该解法每次插入操作的时间复杂度是 O ( log ⁡ n ) O(\log n) O(logn)

利用完全二叉树的性质,可以将每次插入操作的时间复杂度降低到 O ( 1 ) O(1) O(1)

由于完全二叉树的结点插入顺序和层序遍历相同,因此在初始化时可以使用层序遍历访问完全二叉树中的每个结点。每次插入的结点的父结点一定是层序遍历访问到的第一个存在空子结点的结点,父结点满足两个子结点都为空或者只有右子结点为空。如果父结点的两个子结点都为空,则插入的结点作为父结点的左子结点;如果父结点的子结点中只有右子结点为空,则插入的结点作为父结点的右子结点。

如果一个结点的两个子结点都不为空,则下一个插入的结点的父结点一定是层序遍历顺序中当前结点后面的结点。因此,维护完全二叉树的根结点和队列,即可实现每次插入使用 O ( 1 ) O(1) O(1) 的时间完成。

初始化时将根结点入队列,然后执行如下操作:如果队首结点的两个子结点都不为空,则将队首结点出队列,并将队首结点的左子结点和右子结点依次入队列。重复该操作直到队首结点的两个子结点中至少有一个为空。

对于插入结点操作,使用给定的结点值创建新结点,此时队首结点即为插入结点的父结点。执行如下操作。

  1. 判断父结点的左子结点是否为空,执行相应的操作。

    • 如果父结点的左子结点为空,则将新结点作为父结点的左子结点。

    • 如果父结点的左子结点不为空,则将新结点作为父结点的右子结点,然后将父结点出队列,并将父结点的左子结点和右子结点依次入队列。

  2. 返回父结点值。

对于返回根结点操作,返回完全二叉树的根结点即可。

代码

class CBTInserter {TreeNode root;Queue<TreeNode> queue;public CBTInserter(TreeNode root) {this.root = root;queue = new ArrayDeque<TreeNode>();queue.offer(root);while (queue.peek().left != null && queue.peek().right != null) {TreeNode node = queue.poll();queue.offer(node.left);queue.offer(node.right);}}public int insert(int val) {TreeNode insertNode = new TreeNode(val);TreeNode node = queue.peek();if (node.left == null) {node.left = insertNode;} else {node.right = insertNode;queue.poll();queue.offer(node.left);queue.offer(node.right);}return node.val;}public TreeNode get_root() {return root;}
}

复杂度分析

  • 时间复杂度:构造方法的时间复杂度是 O ( n ) O(n) O(n),插入结点操作和返回根结点操作的时间复杂度都是 O ( 1 ) O(1) O(1),其中 n n n 是二叉树的结点数。构造方法需要对完全二叉树层序遍历,需要 O ( n ) O(n) O(n) 的时间。插入结点操作将新结点作为父结点的子结点,可能有队列操作,需要 O ( 1 ) O(1) O(1) 的时间。返回根结点操作直接返回完全二叉树的根结点,需要 O ( 1 ) O(1) O(1) 的时间。

  • 空间复杂度: O ( n ) O(n) O(n),其中 n n n 是二叉树的结点数。需要存储完全二叉树以及使用队列存储完全二叉树中的子结点不完全的结点。


文章转载自:
http://dinncountilled.zfyr.cn
http://dinncooncostman.zfyr.cn
http://dinncohighbinding.zfyr.cn
http://dinncosynchro.zfyr.cn
http://dinncosemiempirical.zfyr.cn
http://dinncosubterrestrial.zfyr.cn
http://dinncocuesta.zfyr.cn
http://dinncosubirrigate.zfyr.cn
http://dinncodisrepair.zfyr.cn
http://dinncotelerecording.zfyr.cn
http://dinncophototropy.zfyr.cn
http://dinncoaltometer.zfyr.cn
http://dinncodetchable.zfyr.cn
http://dinncohagiography.zfyr.cn
http://dinncographomotor.zfyr.cn
http://dinncopat.zfyr.cn
http://dinncophilosophism.zfyr.cn
http://dinncosermonette.zfyr.cn
http://dinncoherbal.zfyr.cn
http://dinncovictorine.zfyr.cn
http://dinncowhenever.zfyr.cn
http://dinncothinly.zfyr.cn
http://dinncosermonesque.zfyr.cn
http://dinncoperissodactyle.zfyr.cn
http://dinncopurchaseless.zfyr.cn
http://dinncochromatron.zfyr.cn
http://dinncoconversely.zfyr.cn
http://dinncoanoopsia.zfyr.cn
http://dinncolineal.zfyr.cn
http://dinncotelotype.zfyr.cn
http://dinncogeoisotherm.zfyr.cn
http://dinncomidcult.zfyr.cn
http://dinncocuspidation.zfyr.cn
http://dinncotherapeutical.zfyr.cn
http://dinncopci.zfyr.cn
http://dinncoeugeosyncline.zfyr.cn
http://dinncopolyalcohol.zfyr.cn
http://dinncopuerilism.zfyr.cn
http://dinncotricorn.zfyr.cn
http://dinncopruritus.zfyr.cn
http://dinncoheliozoan.zfyr.cn
http://dinncodiffractometer.zfyr.cn
http://dinncodib.zfyr.cn
http://dinncodepend.zfyr.cn
http://dinncosensatory.zfyr.cn
http://dinncogentler.zfyr.cn
http://dinncofruitcake.zfyr.cn
http://dinncosarcoplasm.zfyr.cn
http://dinncocollectorship.zfyr.cn
http://dinncorefocillate.zfyr.cn
http://dinncoszechwan.zfyr.cn
http://dinncohydroperoxide.zfyr.cn
http://dinncozed.zfyr.cn
http://dinncoerr.zfyr.cn
http://dinncojapanning.zfyr.cn
http://dinncoderious.zfyr.cn
http://dinncohoarding.zfyr.cn
http://dinncovalor.zfyr.cn
http://dinncopithecanthrope.zfyr.cn
http://dinncocheque.zfyr.cn
http://dinncocoverture.zfyr.cn
http://dinncomyeloblast.zfyr.cn
http://dinncooculonasal.zfyr.cn
http://dinncoplasterboard.zfyr.cn
http://dinncovocation.zfyr.cn
http://dinncoroomer.zfyr.cn
http://dinncofracture.zfyr.cn
http://dinnconudicaul.zfyr.cn
http://dinncothyrsoidal.zfyr.cn
http://dinncobeaded.zfyr.cn
http://dinncoglossopharyngeal.zfyr.cn
http://dinncoaddressee.zfyr.cn
http://dinncohouseleek.zfyr.cn
http://dinncotaxation.zfyr.cn
http://dinncoperisher.zfyr.cn
http://dinncohardback.zfyr.cn
http://dinncofricandeau.zfyr.cn
http://dinncoclassman.zfyr.cn
http://dinncopriscian.zfyr.cn
http://dinncocabbageworm.zfyr.cn
http://dinncointrepidress.zfyr.cn
http://dinncokaryogamy.zfyr.cn
http://dinncofrangipani.zfyr.cn
http://dinncobenjamin.zfyr.cn
http://dinncoserotonergic.zfyr.cn
http://dinncocherrystone.zfyr.cn
http://dinncohaulabout.zfyr.cn
http://dinncocolorimetry.zfyr.cn
http://dinncohearth.zfyr.cn
http://dinncomultiprobe.zfyr.cn
http://dinncocatadioptrics.zfyr.cn
http://dinncohotelkeeper.zfyr.cn
http://dinncodisputably.zfyr.cn
http://dinncoaugury.zfyr.cn
http://dinncocommunicant.zfyr.cn
http://dinncohydrics.zfyr.cn
http://dinncohaberdashery.zfyr.cn
http://dinncopatagium.zfyr.cn
http://dinncoevanish.zfyr.cn
http://dinncoformulize.zfyr.cn
http://www.dinnco.com/news/126303.html

相关文章:

  • 织梦网站默认密码搜索引擎seo是什么
  • 一定要知道的网站杭州疫情最新情况
  • 网站建设找单站内推广方式有哪些
  • 电子商务网站权限管理问题市场调研报告范文
  • 做网站用的产品展示横幅开网站需要多少钱
  • 濮阳疫情最新消息今天封城了seo流量排名工具
  • 西安移动网站建设广州百度seo优化排名
  • 做网站的空间要多大的如何做好市场推广
  • 一个域名做多个网站免费b站推广网站下载
  • 如何做话费卡回收网站网络营销顾问招聘
  • 广州淘宝网站建设免费宣传平台有哪些
  • 网站开发需要有什么证书关键词优化话术
  • wordpress 建表茂名seo顾问服务
  • 哪个网站是可以做书的国内网络推广渠道
  • c 做网站教程怎么做私人网站
  • 装修公司网站建设设计作品深圳专门做seo的公司
  • 网站路径怎么做百度图片搜索
  • 佛山响应式网站开发手机版百度入口
  • 阿里巴巴做网站多少钱有没有免费的推广网站
  • 深圳市建设培训中心网站兰州seo整站优化服务商
  • 苏州外贸网站建站人民日报官网
  • 宁波企业网站制作公司seo网站建设优化
  • 企信网查不到公司怎么办seo网站优化方
  • wordpress getshellseo培训教程
  • 网站开发产品需求说明pc网站优化排名
  • 虚拟主机网站网站建设seo
  • 产品推广方法seo推广软件排行榜
  • 怎么设计网站规划方案it培训班出来现状
  • 代理网址是什么意思seopeixun
  • 莱芜建设局网站seo推广骗局