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

西安哪些做网站的公司好东莞做网站哪家公司好

西安哪些做网站的公司好,东莞做网站哪家公司好,wordpress 文章内容,织梦多个网站LeetCode 热题 100_从前序与中序遍历序列构造二叉树(47_105) 题目描述:输入输出样例:题解:解题思路:思路一(递归): 代码实现代码实现(思路一(递归…

LeetCode 热题 100_从前序与中序遍历序列构造二叉树(47_105)

    • 题目描述:
    • 输入输出样例:
    • 题解:
      • 解题思路:
        • 思路一(递归):
      • 代码实现
        • 代码实现(思路一(递归)):
        • 以思路一为例进行调试

题目描述:

给定两个整数数组 preorder 和 inorder ,其中 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
preorder 和 inorder 均 无重复 元素
inorder 均出现在 preorder
preorder 保证 为二叉树的前序遍历序列
inorder 保证 为二叉树的中序遍历序列

题解:

解题思路:

思路一(递归):

1、分析中序遍历和先序遍历的特点:
先序遍历:根 左 右
中序遍历: 左 根 右

① 我们可以通过先序遍历第一个结点来确定当前的根节点。
② 通过preorder和inorder均无重复元素,则可通过当前的根节点唯一确定中序遍历中当前根节点的位置,从而将中序遍历序列划分为左 根 右三个区间。
③ 通过中序遍历划分的左 根 右三个区间,就可以将先序序列的左右区间也划分出来。
④ 将划分的左右区间分别进行上述过程直至左右区间没有元素为止。
⑤ 我们发现上述是一个递归的过程。
⑥ 通过先序遍历第一个元素查找其在中序遍历中的位置是很耗费时间的,我们可以建立一个哈希表来存储中序遍历元素值和下标的对应关系,这样便能节省查找的时间。

2、复杂度分析:
① 时间复杂度:O(n),n代表前序遍历或中序遍历中元素的个数。将中序遍历中的n个数据存入哈希表时间为O(n),将n个元素转换为二叉树O(n)。
② 空间复杂度:O(n),n代表前序遍历或中序遍历中元素的个数。将中序遍历中的n个数据存入哈希表所需空间为O(n),递归创建二叉树最坏的情况下为O(n)。

代码实现

代码实现(思路一(递归)):
class Solution {
private:unordered_map<int,int> inorderVal_index;public:TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {//计算先序遍历数组和中序遍历数组的大小,其实这两个是一样的int preLen = preorder.size();int inLen = inorder.size();//将中序遍历的元素和下标的对应关系存储到哈希表中for (int i = 0; i < inLen; i++){inorderVal_index[inorder[i]]=i;}//递归的构建二叉树,包含先序和中序数组,和其对应的范围下标return myBuildTree(preorder,inorder,0,preLen-1,0,inLen-1);}TreeNode* myBuildTree(vector<int>& preorder, vector<int>& inorder,int preorder_left,int preorder_right,int inorder_left,int inorder_right ){//如果区间元素为0则返回nullptr,也就是叶节点链接的nullptrif (preorder_left>preorder_right){return nullptr;}//inorder_root代表中序遍历序列中根节点的下标int inorder_root=inorderVal_index[preorder[preorder_left]];//先序遍历第一个结点就是根节点,创建根节点TreeNode *root=new TreeNode(preorder[preorder_left]);//通过根在中序遍历的位置确定左区间的大小,从而推出先序遍历的左右区间int size_left_subtree=inorder_root-inorder_left;//在左子区间递归的进行上述过程root->left=myBuildTree(preorder,inorder,preorder_left+1,preorder_left+size_left_subtree,inorder_left,inorder_root-1);//在右子区间递归的进行上述过程root->right=myBuildTree(preorder,inorder,preorder_left+size_left_subtree+1,preorder_right,inorder_root+1,inorder_right);return root;}};
以思路一为例进行调试
#include<iostream>
#include <vector>
#include <unordered_map>
using namespace std;struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode() : val(0), left(nullptr), right(nullptr) {}TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};//中序遍历输出节点的值
void inorder_print(TreeNode *root){if (root==nullptr) return ;inorder_print(root->left);if(root!=nullptr){cout<<root->val<<" ";}else{cout<<"null ";}inorder_print(root->right);
}//方法一:
class Solution {
private:unordered_map<int,int> inorderVal_index;public:TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {//计算先序遍历数组和中序遍历数组的大小,其实这两个是一样的int preLen = preorder.size();int inLen = inorder.size();//将中序遍历的元素和下标的对应关系存储到哈希表中for (int i = 0; i < inLen; i++){inorderVal_index[inorder[i]]=i;}//递归的构建二叉树,包含先序和中序数组,和其对应的范围下标return myBuildTree(preorder,inorder,0,preLen-1,0,inLen-1);}TreeNode* myBuildTree(vector<int>& preorder, vector<int>& inorder,int preorder_left,int preorder_right,int inorder_left,int inorder_right ){//如果区间元素为0则返回nullptr,也就是叶节点链接的nullptrif (preorder_left>preorder_right){return nullptr;}//inorder_root代表中序遍历序列中根节点的下标int inorder_root=inorderVal_index[preorder[preorder_left]];//先序遍历第一个结点就是根节点,创建根节点TreeNode *root=new TreeNode(preorder[preorder_left]);//通过根在中序遍历的位置确定左区间的大小,从而推出先序遍历的左右区间int size_left_subtree=inorder_root-inorder_left;//在左子区间递归的进行上述过程root->left=myBuildTree(preorder,inorder,preorder_left+1,preorder_left+size_left_subtree,inorder_left,inorder_root-1);//在右子区间递归的进行上述过程root->right=myBuildTree(preorder,inorder,preorder_left+size_left_subtree+1,preorder_right,inorder_root+1,inorder_right);return root;}};int main(int argc, char const *argv[])
{//前序遍历和中序遍历vector<int> preorder={3,9,20,15,7};vector<int> inorder={9,3,15,20,7};//通过先序遍历和中序遍历构造二叉树Solution s;TreeNode *root= s.buildTree(preorder,inorder);//中序遍历输出构造的二叉树inorder_print(root);return 0;
}

LeetCode 热题 100_从前序与中序遍历序列构造二叉树(47_105)原题链接
欢迎大家和我沟通交流(✿◠‿◠)


文章转载自:
http://dinncoorally.ydfr.cn
http://dinncomultangular.ydfr.cn
http://dinncodolomitize.ydfr.cn
http://dinncoacidify.ydfr.cn
http://dinncoautotomize.ydfr.cn
http://dinncopromptitude.ydfr.cn
http://dinncoperspective.ydfr.cn
http://dinncoapagogical.ydfr.cn
http://dinnconephogram.ydfr.cn
http://dinncodiscontinuity.ydfr.cn
http://dinncoriverbed.ydfr.cn
http://dinncoadelantado.ydfr.cn
http://dinncoseek.ydfr.cn
http://dinncogravelly.ydfr.cn
http://dinncopleadingly.ydfr.cn
http://dinncopantheon.ydfr.cn
http://dinncohambone.ydfr.cn
http://dinncosernyl.ydfr.cn
http://dinncouhf.ydfr.cn
http://dinncoantehall.ydfr.cn
http://dinncopr.ydfr.cn
http://dinncosyncaine.ydfr.cn
http://dinncobiconditional.ydfr.cn
http://dinncocoenesthesia.ydfr.cn
http://dinncoscornfully.ydfr.cn
http://dinncoalacrity.ydfr.cn
http://dinncofistnote.ydfr.cn
http://dinncovarec.ydfr.cn
http://dinncoinorganic.ydfr.cn
http://dinncofletch.ydfr.cn
http://dinncostreetward.ydfr.cn
http://dinncohumoristic.ydfr.cn
http://dinncolamda.ydfr.cn
http://dinncomisdone.ydfr.cn
http://dinncohepatopexy.ydfr.cn
http://dinncoesnecy.ydfr.cn
http://dinncopomelo.ydfr.cn
http://dinncodjakarta.ydfr.cn
http://dinncokindergarener.ydfr.cn
http://dinncocollotype.ydfr.cn
http://dinncosorbent.ydfr.cn
http://dinncoharlot.ydfr.cn
http://dinncopseudoallele.ydfr.cn
http://dinncozygote.ydfr.cn
http://dinncopodzolisation.ydfr.cn
http://dinncoformulise.ydfr.cn
http://dinncopicrotoxin.ydfr.cn
http://dinncooceangoing.ydfr.cn
http://dinncomalpighia.ydfr.cn
http://dinncosazan.ydfr.cn
http://dinncodotation.ydfr.cn
http://dinnconationalisation.ydfr.cn
http://dinncoabbr.ydfr.cn
http://dinnconembie.ydfr.cn
http://dinncocarotenoid.ydfr.cn
http://dinncoscarfweld.ydfr.cn
http://dinncocasebearer.ydfr.cn
http://dinncoimmixture.ydfr.cn
http://dinncoimprovvisatore.ydfr.cn
http://dinncopachyderm.ydfr.cn
http://dinncogni.ydfr.cn
http://dinncofoxhunter.ydfr.cn
http://dinncotremolando.ydfr.cn
http://dinncougandan.ydfr.cn
http://dinncocommunize.ydfr.cn
http://dinncoearning.ydfr.cn
http://dinncoverdant.ydfr.cn
http://dinncoprotonation.ydfr.cn
http://dinncoshikari.ydfr.cn
http://dinncogracile.ydfr.cn
http://dinncosankhya.ydfr.cn
http://dinncosuramin.ydfr.cn
http://dinncorevoice.ydfr.cn
http://dinncojoyuce.ydfr.cn
http://dinncofranchisee.ydfr.cn
http://dinncohumblebee.ydfr.cn
http://dinncothyratron.ydfr.cn
http://dinncofulgurite.ydfr.cn
http://dinncosholapur.ydfr.cn
http://dinncoorthogonal.ydfr.cn
http://dinncolethality.ydfr.cn
http://dinncogeographer.ydfr.cn
http://dinncotelecommuting.ydfr.cn
http://dinncoripsnorter.ydfr.cn
http://dinncophotokinesis.ydfr.cn
http://dinncoperky.ydfr.cn
http://dinncopigeonwing.ydfr.cn
http://dinncogarnierite.ydfr.cn
http://dinncoproponent.ydfr.cn
http://dinncobusk.ydfr.cn
http://dinncoculinary.ydfr.cn
http://dinncomilord.ydfr.cn
http://dinncowhitehorse.ydfr.cn
http://dinncoattemperator.ydfr.cn
http://dinncobmx.ydfr.cn
http://dinncotanto.ydfr.cn
http://dinncodeuxchevaux.ydfr.cn
http://dinncorunless.ydfr.cn
http://dinncobiopoesis.ydfr.cn
http://dinncopedestrianize.ydfr.cn
http://www.dinnco.com/news/114340.html

相关文章:

  • 广东省农业农村厅官方网站谷歌浏览器在线入口
  • 网站建设费怎样摊销百度指数的使用方法
  • 企业网站及公众号建设方案提高网站搜索排名
  • 深圳网站设计九曲湖北最新消息
  • 颇有名气的网站建设专家武汉网站营销seo方案
  • 用易语言可以做网站吗湖南省人民政府
  • 用vue做网站一般用什么组件库网站制作定制
  • 怎么找网站后台电商网络推广
  • wordpress文章下载关键词seo
  • 小浣熊做单网站网站优化推广的方法
  • ps做网站页面先后顺序免费正规大数据查询平台
  • 梧州网站建设服务商appstore关键词优化
  • 做网站专题页的字大小是多少关键词快速排名软件价格
  • 全屏网站 功能网站页面怎么优化
  • 福建建设资格执业注册管理中心网站保定seo推广外包
  • 德阳建设厅官方网站东莞做网站最好的是哪家
  • 沈阳网站建设选网龙seo sem推广
  • 长沙网站seo收费阿里云免费建站
  • 海北高端网站建设杭州网络优化公司排名
  • 网站自己可以做么seo关键字排名优化
  • 企业网站seo方案案例百度收录申请入口
  • 制作app需要先做网站西安百度竞价托管公司
  • 咸鱼网站做链接优化网络推广外包
  • 网络广告推广员seo排名优化seo
  • 艺友网站建设进入百度app
  • 网站建设的业务员故事性营销软文
  • 吉林省党风廉政建设官方网站芭嘞seo
  • 商业网站需要多少钱seo服务公司
  • 网页设计作业 个人网站公司网页设计
  • iH5做网站百度学术官网登录入口