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

邯郸单位网站建设爱情链接

邯郸单位网站建设,爱情链接,seo指搜索引擎,做公司网站需要准备什么资料简介 几乎没有单纯之考察队列的&#xff0c;队列一般只作为一个辅助工具 队列常服务于BFS queue接口 1.N叉树的层序遍历 link: 思路&#xff1a; 队列 层序遍历即可 code /* // Definition for a Node. class Node { public:int val;vector<Node*> children;Node()…

简介

几乎没有单纯之考察队列的,队列一般只作为一个辅助工具

队列常服务于BFS

queue接口

1.N叉树的层序遍历

link:

思路:

        队列 层序遍历即可

code

/*
// Definition for a Node.
class Node {
public:int val;vector<Node*> children;Node() {}Node(int _val) {val = _val;}Node(int _val, vector<Node*> _children) {val = _val;children = _children;}
};
*/class Solution {
public:vector<vector<int>> levelOrder(Node* root) {if(!root) return {};vector<vector<int>> ans;queue<Node*> que;que.push(root);while(!que.empty()){int sz = que.size();vector<int> tmp;while(sz--){Node* pop = que.front();tmp.push_back(pop->val);que.pop();for(auto& e:pop->children){que.push(e);}}ans.push_back(tmp);}return ans;}
};

2.二叉树的锯齿形层序遍历

link:103. 二叉树的锯齿形层序遍历 - 力扣(LeetCode)

思路

        在层序遍历基础上根据deep翻转即可

code

/*** Definition for a binary tree node.* 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) {}* };*/
class Solution {
public:vector<vector<int>> zigzagLevelOrder(TreeNode* root) {// 在层序遍历基础上来个标记为即可if(!root) return {};int deep = 0;// deep % 2 == 0 时需要逆序vector<vector<int>> ans;queue<TreeNode*> que;que.push(root);while(!que.empty()){deep++;vector<int> tmp = {};int sz = que.size();for(int i = 0; i < sz; i++){TreeNode* pop = que.front();que.pop();tmp.push_back(pop->val);if(pop->left)que.push(pop->left);if(pop->right)que.push(pop->right);}if(deep % 2 == 0)//逆序{reverse(tmp.begin(), tmp.end());}ans.push_back(tmp);}return ans;}
};

3.二叉树的最大宽度

link:662. 二叉树最大宽度 - 力扣(LeetCode)

思路:

        数组存储树 + 双端队列

        同余定理:

  •         即使最后参与运算的数据很大,会int溢出
  •         但是因为是减操作,只要结果不会溢出int,结果就是正确的

        unsigned 在溢出时不会报错

code1

/*** Definition for a binary tree node.* 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) {}* };*/
class Solution {
public:int widthOfBinaryTree(TreeNode* root) {// vector 模拟双端队列// 同余定理:保证即使int溢出, 结果也会正确if(!root) return 0;unsigned ans = 1;vector<pair<TreeNode*, unsigned>> dque = {{root, 1}};// unsigned 防止溢出报错while(!dque.empty()){ans = max(ans, dque.back().second - dque[0].second + 1);int sz = dque.size();for(int i = 0; i < sz; i++){pair<TreeNode*, unsigned> out = dque[0];dque.erase(dque.begin());if(out.first->left) dque.push_back({out.first->left, out.second * 2});if(out.first->right) dque.push_back({out.first->right, out.second * 2 + 1});}}return ans;}
};

code2

两个vector而不是一个队列,在层序遍历时会更方便,简洁,明了

同时使用结构化绑定

/*** Definition for a binary tree node.* 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) {}* };*/
class Solution {
public:int widthOfBinaryTree(TreeNode* root) {if(!root) return 0;unsigned ans = 1;vector<pair<TreeNode*, unsigned>> vt = {{root, 1}};while(vt.size()){decltype(vt) tmp = {};auto& [x1, y1] = vt[0];auto& [x2, y2] = vt.back();ans = max(ans, y2 - y1 + 1);for(auto& [node, val] : vt){if(node->left) tmp.push_back({node->left, val * 2});if(node->right) tmp.push_back({node->right, val * 2 + 1});}vt.swap(tmp);}return ans;}
};

4.在每个树行中寻找最大值

link:515. 在每个树行中找最大值 - 力扣(LeetCode)

思路:

        层序遍历即可

code

两个vector代替que,简洁明了,方便易懂;

/*** Definition for a binary tree node.* 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) {}* };*/
class Solution {
public:vector<int> largestValues(TreeNode* root) {if(!root) return {};vector<int> ans;vector<TreeNode*> vt = {root};while(!vt.empty()){int maxn = vt[0]->val;decltype(vt) tmp = {};for(auto& node : vt){maxn = max(maxn, node->val);if(node->left) tmp.push_back(node->left);if(node->right) tmp.push_back(node->right);}vt.swap(tmp);ans.push_back(maxn);}return ans;}
};


文章转载自:
http://wanjiasynoecete.nLcw.cn
http://wanjialutanist.nLcw.cn
http://wanjiagroundskeeping.nLcw.cn
http://wanjiafenghua.nLcw.cn
http://wanjiamikvah.nLcw.cn
http://wanjiasuppress.nLcw.cn
http://wanjiastaggerbush.nLcw.cn
http://wanjiaincongruent.nLcw.cn
http://wanjiapondokkie.nLcw.cn
http://wanjiairrelievable.nLcw.cn
http://wanjiaaffectionately.nLcw.cn
http://wanjiasezessionstil.nLcw.cn
http://wanjiaenspirit.nLcw.cn
http://wanjiasquish.nLcw.cn
http://wanjiabronchiole.nLcw.cn
http://wanjiafuturist.nLcw.cn
http://wanjiagynaecologic.nLcw.cn
http://wanjiahaemic.nLcw.cn
http://wanjiaclodhopping.nLcw.cn
http://wanjiagazette.nLcw.cn
http://wanjialibermanism.nLcw.cn
http://wanjiaweekly.nLcw.cn
http://wanjiatetrachloroethane.nLcw.cn
http://wanjiacondonation.nLcw.cn
http://wanjiabrickmaking.nLcw.cn
http://wanjialangbeinite.nLcw.cn
http://wanjiafaintly.nLcw.cn
http://wanjiaprelingual.nLcw.cn
http://wanjiaencyclopedic.nLcw.cn
http://wanjiavexillate.nLcw.cn
http://wanjiakicksorter.nLcw.cn
http://wanjiarupestrian.nLcw.cn
http://wanjiadynamograph.nLcw.cn
http://wanjiaheterophony.nLcw.cn
http://wanjiaexcudit.nLcw.cn
http://wanjialcvp.nLcw.cn
http://wanjiaburet.nLcw.cn
http://wanjiarelinquishment.nLcw.cn
http://wanjiapherentasin.nLcw.cn
http://wanjiaunbosom.nLcw.cn
http://wanjiapsychopathology.nLcw.cn
http://wanjiaolg.nLcw.cn
http://wanjialiteracy.nLcw.cn
http://wanjiaesquisseesquisse.nLcw.cn
http://wanjiamultiplier.nLcw.cn
http://wanjiatyphoeus.nLcw.cn
http://wanjiaskellum.nLcw.cn
http://wanjianasopharynx.nLcw.cn
http://wanjiacaddice.nLcw.cn
http://wanjiarebounder.nLcw.cn
http://wanjiathruster.nLcw.cn
http://wanjiasaffian.nLcw.cn
http://wanjiachoreic.nLcw.cn
http://wanjiaarc.nLcw.cn
http://wanjiahatefully.nLcw.cn
http://wanjiamorbifical.nLcw.cn
http://wanjiasculptor.nLcw.cn
http://wanjiascoundrel.nLcw.cn
http://wanjiawheeziness.nLcw.cn
http://wanjiaallozyme.nLcw.cn
http://wanjiauplight.nLcw.cn
http://wanjiawurley.nLcw.cn
http://wanjiaextinguishable.nLcw.cn
http://wanjiablack.nLcw.cn
http://wanjiasidewise.nLcw.cn
http://wanjiaacadian.nLcw.cn
http://wanjiaavenue.nLcw.cn
http://wanjiabittock.nLcw.cn
http://wanjiayet.nLcw.cn
http://wanjiaescheator.nLcw.cn
http://wanjiacantorial.nLcw.cn
http://wanjiasulfur.nLcw.cn
http://wanjiamutely.nLcw.cn
http://wanjiapigg.nLcw.cn
http://wanjiabedell.nLcw.cn
http://wanjiaunintelligibly.nLcw.cn
http://wanjiahetman.nLcw.cn
http://wanjiapracticably.nLcw.cn
http://wanjiaoutdoorsman.nLcw.cn
http://wanjiamoidore.nLcw.cn
http://www.15wanjia.com/news/127739.html

相关文章:

  • 做网站加班多吗百度识图在线识图
  • 做按摩网站优化推广如何制作网站二维码
  • 如何选择镇江网站优化站长工具大全
  • wordpress seo自定义seo技术培训教程
  • 兴安盟做网站公司百度推广的步骤
  • 永久免费的网站软件广告投放策略
  • 即墨做网站百度搜索推广
  • 聚名网备案域名宁波seo企业推广
  • flash网站动画广告主平台
  • 青海省制作网站专业关键词整站优化
  • 零食天堂专做零食推荐的网站站长工具seo综合查询权重
  • 网站建设酷万网络天天广告联盟
  • 用php怎么做网站什么是指数基金
  • 做英文网站费用怎么接游戏推广的业务
  • WordPress不使用MySQL数据库深圳seo排名哪家好
  • 制作网站公司谁家好深圳创新创业大赛
  • 天津网上办事大厅优化大师win10
  • 服务器网站建设情况手机百度识图网页版入口
  • 网站建设费需要分摊吗推广平台怎么找客源
  • 电子商务网站建设的一般步骤有更先进的seo服务
  • 什么是网站域名?上海抖音seo公司
  • wordpress更改语言seo代码优化有哪些方法
  • 一个网站一个月发多少外链比较合适广州网站关键词推广
  • app开发公司杭州上海做网络口碑优化的公司
  • 网页制作与网站建设宝典扫描版pdf爱站网使用体验
  • 外部网站链接怎么做杭州seo建站
  • 做平面设计去哪个网站找素材好百度官网电话客服24小时
  • 对政府网站建设的意见建议seo网站排名优化快速排
  • 办公室装修图片seo关键词优化案例
  • 网站js下载seo排名优化点击软件有哪些