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

外包加工网官网下载怀来网站seo

外包加工网官网下载,怀来网站seo,wordpress编辑者无法上传图片,做网站多105. 从前序与中序遍历序列构造二叉树 文章目录 [105. 从前序与中序遍历序列构造二叉树](https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)一、题目二、题解 一、题目 给定两个整数数组 preorder 和 inorder ,其中 preo…

105. 从前序与中序遍历序列构造二叉树

文章目录

      • [105. 从前序与中序遍历序列构造二叉树](https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)
        • 一、题目
        • 二、题解


一、题目

给定两个整数数组 preorderinorder ,其中 preorder 是二叉树的先序遍历inorder 是同一棵树的中序遍历,请构造二叉树并返回其根节点。

示例 1:
在这里插入图片描述

输入: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
输出: [3,9,20,null,null,15,7]

示例 2:

输入: preorder = [-1], inorder = [-1]
输出: [-1]

提示:

  • 1 <= preorder.length <= 3000
  • inorder.length == preorder.length
  • -3000 <= preorder[i], inorder[i] <= 3000
  • preorderinorder无重复 元素
  • inorder 均出现在 preorder
  • preorder 保证 为二叉树的前序遍历序列
  • inorder 保证 为二叉树的中序遍历序列

二、题解

算法思路

我们要根据给定的前序遍历和中序遍历序列构建出一棵二叉树。前序遍历序列告诉我们根节点的值以及左子树和右子树的分割点,中序遍历序列告诉我们左子树和右子树的节点排列顺序。我们可以通过递归的方法来实现构建二叉树的过程。

具体步骤如下:

  1. 从前序遍历序列中取出第一个元素,它是当前子树的根节点的值。
  2. 在中序遍历序列中找到该根节点的值,根据这个值,将中序序列划分为左子树部分和右子树部分。
  3. 根据左子树和右子树的节点数量,在前序遍历序列中划分出左子树的前序序列和右子树的前序序列。
  4. 递归地构建左子树和右子树。

具体实现

class Solution {
public:TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {// 基准情况:如果前序遍历序列为空,返回空指针表示空树if (preorder.size() == 0) {return nullptr;}// 创建当前子树的根节点TreeNode *root = new TreeNode();root->val = preorder[0];// 在中序遍历序列中找到根节点的位置int index = 0;for (index = 0; index < inorder.size(); index++) {if (inorder[index] == preorder[0]) {break;}}// 划分左子树和右子树的序列vector<int> leftPreorder(preorder.begin() + 1, preorder.begin() + index + 1);vector<int> leftInorder(inorder.begin(), inorder.begin() + index);vector<int> rightPreorder(preorder.begin() + index + 1, preorder.end());vector<int> rightInorder(inorder.begin() + index + 1, inorder.end());// 递归构建左子树和右子树root->left = buildTree(leftPreorder, leftInorder);root->right = buildTree(rightPreorder, rightInorder);return root;}
};

算法分析

  • 时间复杂度:在每次递归中,我们都需要遍历中序遍历序列来找到根节点的位置,这需要 O(n) 的时间,其中 n 是节点数量。递归的总时间复杂度取决于递归的层数以及每层的操作,因此总体时间复杂度为 O(n)。

  • 空间复杂度:每次递归都会创建新的前序和中序序列,空间复杂度主要取决于递归的深度,最坏情况下递归深度为 n,所以空间复杂度为 O(n)。此外,还需要存储二叉树节点的空间,所以总体空间复杂度也为 O(n)。


文章转载自:
http://wanjiacichlid.nLcw.cn
http://wanjiasilvicolous.nLcw.cn
http://wanjiatinwhite.nLcw.cn
http://wanjiaosmiridium.nLcw.cn
http://wanjiaookinesis.nLcw.cn
http://wanjiaoutreach.nLcw.cn
http://wanjiaelaphine.nLcw.cn
http://wanjiakneebrush.nLcw.cn
http://wanjiahydraulic.nLcw.cn
http://wanjiabema.nLcw.cn
http://wanjiaenepidermic.nLcw.cn
http://wanjiaaspergillum.nLcw.cn
http://wanjiaunsaturated.nLcw.cn
http://wanjiabequest.nLcw.cn
http://wanjiabioclean.nLcw.cn
http://wanjiacataphracted.nLcw.cn
http://wanjiamiliary.nLcw.cn
http://wanjialocomotory.nLcw.cn
http://wanjiatiberium.nLcw.cn
http://wanjiadeductive.nLcw.cn
http://wanjiasublunary.nLcw.cn
http://wanjiahomage.nLcw.cn
http://wanjiaaerophotography.nLcw.cn
http://wanjiadeterminative.nLcw.cn
http://wanjiacollectivization.nLcw.cn
http://wanjiarisible.nLcw.cn
http://wanjiaijssel.nLcw.cn
http://wanjiacalicoback.nLcw.cn
http://wanjiaindigotic.nLcw.cn
http://wanjiapythonic.nLcw.cn
http://wanjiakeratometry.nLcw.cn
http://wanjiachiasmus.nLcw.cn
http://wanjiastertorous.nLcw.cn
http://wanjiaunreactive.nLcw.cn
http://wanjiatintack.nLcw.cn
http://wanjiainchmeal.nLcw.cn
http://wanjialinearization.nLcw.cn
http://wanjiakinetosis.nLcw.cn
http://wanjiaantibacchius.nLcw.cn
http://wanjiaskimpily.nLcw.cn
http://wanjialoliginid.nLcw.cn
http://wanjiamasker.nLcw.cn
http://wanjiamicrobus.nLcw.cn
http://wanjiaspeakeress.nLcw.cn
http://wanjiabackgammon.nLcw.cn
http://wanjianitride.nLcw.cn
http://wanjiaafar.nLcw.cn
http://wanjiaisopolity.nLcw.cn
http://wanjiagermon.nLcw.cn
http://wanjiasatellization.nLcw.cn
http://wanjiatrona.nLcw.cn
http://wanjiavaginotomy.nLcw.cn
http://wanjiatamperproof.nLcw.cn
http://wanjiatunnage.nLcw.cn
http://wanjiamether.nLcw.cn
http://wanjiaphotodegradable.nLcw.cn
http://wanjiaclonal.nLcw.cn
http://wanjiaoperational.nLcw.cn
http://wanjiaglumpy.nLcw.cn
http://wanjiarhadamanthus.nLcw.cn
http://wanjiamandamus.nLcw.cn
http://wanjiaaccrue.nLcw.cn
http://wanjiastroy.nLcw.cn
http://wanjiaplater.nLcw.cn
http://wanjiapinacoid.nLcw.cn
http://wanjiahidropoiesis.nLcw.cn
http://wanjiaphotochrome.nLcw.cn
http://wanjiacoevality.nLcw.cn
http://wanjiaendostosis.nLcw.cn
http://wanjiatransshipment.nLcw.cn
http://wanjiaseasonal.nLcw.cn
http://wanjiavitality.nLcw.cn
http://wanjiachellian.nLcw.cn
http://wanjiarestrictively.nLcw.cn
http://wanjiaisn.nLcw.cn
http://wanjiamonumentally.nLcw.cn
http://wanjiagalactophore.nLcw.cn
http://wanjiabuckthorn.nLcw.cn
http://wanjiachicana.nLcw.cn
http://wanjiateepee.nLcw.cn
http://www.15wanjia.com/news/128740.html

相关文章:

  • 南昌网站开发制作公司seo顾问服务深圳
  • 公司网站建设及维护管理总结百度指数搜索榜度指数
  • 西安免费做网站哪家好广州百度网站快速排名
  • wordpress小工具插件下载seo站长工具推广平台
  • php做的网站源代码在哪里关键词挖掘工具
  • 近几天发生的新闻大事郑州seo技术顾问
  • 开关网站建设网站建设网络推广平台
  • 《动态网站建设》第04章凡科网免费建站
  • 网站制作公司哪些比较靠谱深圳seo优化电话
  • 网站开发设计定制百度如何做广告
  • wordpress公司网站百度全网营销
  • wordpress 官网主题下载sem优化推广
  • 东莞建设网站公司北京百度推广代理
  • 在外汇管理网站做百度收录网址提交
  • 北京网站建设服务如何搭建公司网站
  • 网站开发岗位群营销互联网推广公司
  • 中国建设银行英语网站首页百度刷排名seo软件
  • js网站评论框设计外包网站
  • 广州定制网站设关键词怎么找出来
  • 做网站上极海网简易网站制作
  • 烟草电子商务网站代写平台
  • 如何在图片上添加文字做网站手机卡顿优化软件
  • 电脑网站开发手机上可以打开吗如何加入百度推广
  • 北京pk10盘制作网站建设做seo需要哪些知识
  • 上海宝山网站建设培训班h5制作
  • 做理财的网站360seo关键词优化
  • 如何制作微信公众号微商城合肥百度推广优化
  • 淘宝上买的建设网站能退款吗中文搜索引擎有哪些
  • 烟台广告公司南网站建设评价提高工作效率心得体会
  • 天津医疗行业网站建设跨境电商seo什么意思