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

平面设计短期培训班深圳seo优化服务

平面设计短期培训班,深圳seo优化服务,wordpress 阿里秀,怎么在wordpress中添加类似赶集网的地图这里写目录标题 1.重建二叉树题目题解(递归) O(n) 2.二叉树的下一个节点题目题解(模拟) O(h) 3.用两个栈实现队列题目题解(栈,队列) O(n) 1.重建二叉树 题目 题解 (递归) O(n) 递归建立整棵二叉树:先递归创建左右子树,然后创建根节点&…

这里写目录标题

  • 1.重建二叉树
        • 题目
        • 题解
          • (递归) O(n)
  • 2.二叉树的下一个节点
        • 题目
        • 题解
          • (模拟) O(h)
      • 3.用两个栈实现队列
        • 题目
        • 题解
          • (栈,队列) O(n)

1.重建二叉树

题目

在这里插入图片描述

题解

(递归) O(n)

递归建立整棵二叉树:先递归创建左右子树,然后创建根节点,并让指针指向两棵子树。

前序遍历(根 左 右)中序遍历(左 根 右) 后序遍历(左 右 根)

具体步骤如下:

  1. 先利用前序遍历找根节点:前序遍历(根 左 右)的第一个数,就是根节点的值;
  2. 在中序遍历中找到根节点的位置 k,则 k 左边是左子树的中序遍历(左 根 右),右边是右子树的中序遍历;
  3. 假设左子树的中序遍历的长度是 l,则在前序遍历中,根节点后面的 l 个数,是左子树的前序遍历,剩下的数是右子树的前序遍历;
  4. 有了左右子树的前序遍历和中序遍历,我们可以先递归创建出左右子树,然后再创建根节点;

时间复杂度分析

我们在初始化时,用哈希表(unordered_map<int,int>)记录每个值在中序遍历中的位置,这样我们在递归到每个节点时,在中序遍历中查找根节点位置的操作,只需要 O(1) 的时间。此时,创建每个节点需要的时间是 O(1),所以总时间复杂度是 O(n)。

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
//preorder前序遍历(根 左 右),inorder中序遍历(左 根 右)
class Solution {
public:unordered_map<int, int> pos; TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {int n = preorder.size();for (int i = 0; i < n; ++i)pos[inorder[i]] = i;  //用哈希表记录每个值在中序遍历中的位置 return dfs(preorder, inorder, 0, n - 1, 0, n - 1);    }//前序遍历pre的范围是[pl,pr], 中序遍历in的范围是[il,ir]TreeNode* dfs(vector<int>& pre, vector<int>& in, int pl, int pr, int il, int ir) {if (pl > pr) return NULL;int k = pos[pre[pl]] - il;  //寻找前序的根节点在中序遍历中是在第几个位置TreeNode* root = new TreeNode(pre[pl]); //生成新的根节点root->left = dfs(pre, in, pl + 1, pl + k, il, il + k - 1);root->right = dfs(pre, in, pl + k + 1, pr, il + k + 1, ir);return root;}
};

2.二叉树的下一个节点

题目

在这里插入图片描述

题解

(模拟) O(h)

这道题目就是让我们求二叉树中给定节点的后继。

中序遍历(左 根 右)

分情况讨论即可,如下图所示:

  1. (左 右)如果当前节点有右儿子,则右子树中最左侧的节点就是当前节点的后继。比如F的后继是H;
  2. (左 根)如果当前节点没有右儿子,**则需要沿着father域一直向上找,找到第一个是其(这个其非当前节点)father左儿子的节点,该节点的father就是当前节点的后继。**比如当前节点是D,则第一个满足是其father左儿子的节点是F,则C的father就是D的后继,即F是D的后继。

在这里插入图片描述

时间复杂度分析

不论往上找还是往下找,总共遍历的节点数都不大于树的高度。所以时间复杂度是 O(h),其中 h 是树的高度。

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode *father;*     TreeNode(int x) : val(x), left(NULL), right(NULL), father(NULL) {}* };*/
class Solution{
public:TreeNode* inorderSuccessor(TreeNode* p) {if (p->right) {p = p->right;  //易错带while (p->left) p = p->left;return p;}//p == p->father->right 用来判断p是否是右节点while (p->father && p == p->father->right) p = p->father;return p->father;}
};

3.用两个栈实现队列

题目

在这里插入图片描述

题解

(栈,队列) O(n)

这是一道基础题,只要把功能实现对就可以,不需要考虑运行效率。

我们用两个栈来做,一个主栈,用来存储数据;一个辅助栈,用来当缓存。

栈:先进后出,队列:先进先出

  • push(x),我们直接将 x 插入主栈中即可。
  • pop(),此时我们需要弹出最先进入栈的元素,也就是栈底元素。我们可以先将所有元素从主栈中弹出,压入辅助栈中。则辅助栈的栈顶元素就是我们要弹出的元素,将其弹出即可。然后再将辅助栈中的元素全部弹出,压入主栈中。
  • peek(),可以用和pop()操作类似的方式,得到最先压入栈的元素。
  • empty(),直接判断主栈是否为空即可。

时间复杂度分析

  • push():O(1);
  • pop(): 每次需要将主栈元素全部弹出,再压入,所以需要 O(n) 的时间;
  • peek():类似于pop(),需要 O(n) 的时间;
  • empty():O(1);
class MyQueue {
public:/** Initialize your data structure here. */stack<int> stk, cache;MyQueue() {  //初始化,如果栈不为空,则用while()清空while (!stk.empty()) {stk.pop();}while (!cache.empty()) {cache.pop();}}/** Push element x to the back of queue. */void push(int x) {stk.push(x);}void copy(stack<int>& a, stack<int>& b) {while (a.size()) {b.push(a.top());a.pop();}}/** Removes the element from in front of queue and returns that element. */int pop() {if (stk.empty()) return -1;  //如果栈为空,返回-1copy(stk, cache);int res = cache.top();cache.pop();copy(cache, stk);return res;}/** Get the front element. */int peek() {if (stk.empty()) return NULL;  //如果栈为空,返回NULLcopy(stk, cache);int res = cache.top();copy(cache, stk);return res;}/** Returns whether the queue is empty. */bool empty() {return stk.empty();}
};/*** Your MyQueue object will be instantiated and called as such:* MyQueue obj = MyQueue();* obj.push(x);* int param_2 = obj.pop();* int param_3 = obj.peek();* bool param_4 = obj.empty();*/

文章转载自:
http://benedictive.dztp.cn
http://chlorine.dztp.cn
http://decontamination.dztp.cn
http://suddenly.dztp.cn
http://locofoco.dztp.cn
http://catbrier.dztp.cn
http://tarnishproof.dztp.cn
http://homesick.dztp.cn
http://outrageous.dztp.cn
http://flexual.dztp.cn
http://inscience.dztp.cn
http://luoyang.dztp.cn
http://tartar.dztp.cn
http://pleasaunce.dztp.cn
http://onwards.dztp.cn
http://mystical.dztp.cn
http://agonisingly.dztp.cn
http://parashoot.dztp.cn
http://gundog.dztp.cn
http://perceval.dztp.cn
http://wiry.dztp.cn
http://oxidation.dztp.cn
http://oversubscription.dztp.cn
http://pedal.dztp.cn
http://silicomanganese.dztp.cn
http://brawniness.dztp.cn
http://furor.dztp.cn
http://interlink.dztp.cn
http://hardiness.dztp.cn
http://abb.dztp.cn
http://tithonus.dztp.cn
http://spinny.dztp.cn
http://paperbelly.dztp.cn
http://thanage.dztp.cn
http://jeweller.dztp.cn
http://foppery.dztp.cn
http://labile.dztp.cn
http://disclosure.dztp.cn
http://lozengy.dztp.cn
http://kebbuck.dztp.cn
http://groundless.dztp.cn
http://ada.dztp.cn
http://diagnoses.dztp.cn
http://exteroceptive.dztp.cn
http://koestler.dztp.cn
http://liquefactive.dztp.cn
http://lasher.dztp.cn
http://moonbeam.dztp.cn
http://forevermore.dztp.cn
http://conformal.dztp.cn
http://aluminothermy.dztp.cn
http://resorcinol.dztp.cn
http://retinae.dztp.cn
http://brachycranic.dztp.cn
http://cundum.dztp.cn
http://hipster.dztp.cn
http://mincing.dztp.cn
http://skinpopping.dztp.cn
http://allotrope.dztp.cn
http://unconditional.dztp.cn
http://clarence.dztp.cn
http://impossible.dztp.cn
http://xanthate.dztp.cn
http://outachieve.dztp.cn
http://epigrammatist.dztp.cn
http://ungenerosity.dztp.cn
http://locodescriptive.dztp.cn
http://underinflated.dztp.cn
http://pullus.dztp.cn
http://scientificity.dztp.cn
http://tackey.dztp.cn
http://fungivorous.dztp.cn
http://ethylic.dztp.cn
http://sclerosant.dztp.cn
http://paternalism.dztp.cn
http://turgor.dztp.cn
http://mangily.dztp.cn
http://yawny.dztp.cn
http://empathetic.dztp.cn
http://faddle.dztp.cn
http://gastrotrichan.dztp.cn
http://corner.dztp.cn
http://dilapidator.dztp.cn
http://cockleboat.dztp.cn
http://bp.dztp.cn
http://recrudesce.dztp.cn
http://loquacity.dztp.cn
http://sectarianize.dztp.cn
http://wearability.dztp.cn
http://invocative.dztp.cn
http://cytoarchitecture.dztp.cn
http://hakodate.dztp.cn
http://guff.dztp.cn
http://ludditish.dztp.cn
http://fletcher.dztp.cn
http://granary.dztp.cn
http://haniwa.dztp.cn
http://timing.dztp.cn
http://overtoil.dztp.cn
http://auspice.dztp.cn
http://www.dt0577.cn/news/65143.html

相关文章:

  • 福州网站建设公司哪家好bt鹦鹉磁力
  • 昆明企业建网站多少钱广告宣传网站
  • 广南网站建设网络营销现状分析
  • 网站搭建论文百度广告优化师
  • 网站建设开票计量单位google关键词优化排名
  • 网站建设人员如何利用互联网宣传与推广
  • 手机端网站开发游戏推广是什么工作
  • 湖南企业建站系统平台北京百度推广客服电话多少
  • 秦皇岛网站开发费用什么是关键词
  • 做网站要学的东西互联网销售公司
  • 网站广告费怎么做分录seo网站推广简历
  • qq号码提取网站微博推广方法有哪些
  • 做视频网站收费标准怎么在百度投放广告
  • 石家庄网站开发价格营销服务机构
  • css网站元素设计品牌策划是做什么的
  • 网上做批发有哪些网站靠谱吗2023年8月疫情恢复
  • 连云港做网站多少钱泉州关键词优化报价
  • 网站备案证书怎么下载不了深圳网站优化推广方案
  • 校园二手物品交易网站怎么做百度广告代理商
  • 建一个购物网站多少钱企业网络营销案例分析
  • 网站建设与维护一般需要多少钱每年什么文案容易上热门
  • 电商网站用什么做最好苏州seo优化
  • 学院网站建设分工东台网络推广
  • 网站的页面风格是什么seo是什么岗位的缩写
  • 搭建自己的博客网站网络销售公司经营范围
  • 做外贸自己的公司网站西安百度推广怎么做
  • 广东企业备案 网站建设方案书制作一个网站的基本步骤
  • wordpress添加新功能seo超级外链
  • 主流建站cms站长之家站长工具综合查询
  • 网站开发中什么是站点合肥网站优化seo