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

网站开发项目描述广州学建网站

网站开发项目描述,广州学建网站,石家庄哪有个人建站的,百度知道首页登录Problem: 199. 二叉树的右视图 文章目录 题目描述思路解题方法复杂度Code 题目描述 思路 无论是DFS还是BFS我们都要思考到达二叉树的每一层#xff08;或者每一层中的每一个节点#xff09;时#xff0c;我们都该如何按题目要求做出对应得处理!!!在本体中我们主要是#x… Problem: 199. 二叉树的右视图 文章目录 题目描述思路解题方法复杂度Code 题目描述 思路 无论是DFS还是BFS我们都要思考到达二叉树的每一层或者每一层中的每一个节点时我们都该如何按题目要求做出对应得处理!!!在本体中我们主要是 1.当右子树与左子树等高或者右子树高于左子树时我们只添加每一个右子树得右节点到结果集根节点得左子树整个都去除 2.当左子树高于右子树时我们将等高得部分按1中处理左子树高出右子树得部分再将其右子树得右节点添加到结果集 解题方法 思路1DFS 1.创建unordered_mapint, int rightmostValueAtDepth;记录每一层应该添加到右视图的节点int maxDepth -1;记录并维护当前的最大深度stackTreeNode nodeStack;用于DFS过程中的存储节点stack depthStack;用于同时记录DFS过程中的树的深度 2.将根节点添加到nodeStack中while循环遍历循环退出条件为nodeStack为空每次弹出nodeStack和depthStack中的栈顶元素node与depth若此时node不为空则更新最大深度同时若rightmostValueAtDepth[depth]不存在则添加到rightmostValueAtDepth中*并且将node - left;node - rightdepth 1;depth 1分别添加到对应的nodeStack和depthStack栈 3.最后将rightmostValueAtDepth中的值添加到一个一维数组中即可 思路2BFS 大体实现直接套用BFS代码的模板书写即可具体解释下面代码实现中的两个点 1.由于常规的BFS模板均是先添加左子树节点到队列再添加右子树节点到队列所以我们可以利用数组元素可以覆盖的特性在每次添加节点到队列时也将该节点值放入一个空间大小为1的数组temp中这样操作后无论是思路中1、2哪种情况都能保证最后添加到结果集中的节点值是复合题目右视图这个定义 2.按常规BFS代码的实现或者说就是按下面代码前面部分的操作会导致最后一个被写到temp数组中的元素会添加两次到最终的结果集中所以要push_back一次!!! 复杂度 思路1、2均如下 时间复杂度: O ( n ) O(n) O(n) 空间复杂度: O ( n ) O(n) O(n) Code 思路1 class Solution { public:/*** DFS** param root The root of a binary tree* return vectorint*/vectorint rightSideView(TreeNode *root) {if (root nullptr) {return {};}unordered_mapint, int rightmostValueAtDepth;int maxDepth -1;stackTreeNode * nodeStack;stackint depthStack;nodeStack.push(root);depthStack.push(0);while (!nodeStack.empty()) {TreeNode *node nodeStack.top();nodeStack.pop();int depth depthStack.top();depthStack.pop();if (node ! nullptr) {//Maintain the maximum depth of a binary treemaxDepth max(maxDepth, depth);//If not in the map collection, add itif (rightmostValueAtDepth.find(maxDepth) rightmostValueAtDepth.end()) {rightmostValueAtDepth[depth] node-val;}nodeStack.push(node-left);nodeStack.push(node-right);depthStack.push(depth 1);depthStack.push(depth 1);}}vectorint res;for (int i 0; i maxDepth; i) {res.push_back(rightmostValueAtDepth[i]);}return res;} }; 思路2 class Solution { public:/*** BFS* * param root The root of a binary tree* return vectorint*/vectorint rightSideView(TreeNode* root) {if (root nullptr) {return {};}if (root - left nullptr root - right nullptr) {return {root - val};}vectorint res;vectorint temp(1);queueTreeNode* queue;res.push_back(root - val);queue.push(root);while (!queue.empty()) {int curLevelSize queue.size();for (int i 0; i curLevelSize; i) {TreeNode* curLevelNode queue.front();queue.pop();if (curLevelNode - left ! nullptr) {temp[0] curLevelNode - left - val;queue.push(curLevelNode - left);}if (curLevelNode - right ! nullptr) {temp[0] curLevelNode - right - val;queue.push(curLevelNode - right);}}res.push_back(temp[0]);}//Pop the last repetitive noderes.pop_back();return res;} };
http://www.hkea.cn/news/14434538/

相关文章:

  • 排名好的青岛网站建设网站建设咨询费用
  • 网站建设怎么搞微信如何建网站
  • 嘉兴建站公司夏邑县百城建设提质网站
  • 哈尔滨市做淘宝的网站网络架构和网络拓扑的区别
  • 山西建网站商品门户网站解决方案
  • 建论坛网站多少钱温州网站升级
  • 骨干专业建设网站wordpress数据库链接
  • 做网站电话沧州宁波公司网站开发招聘
  • 物流企业网站建设策划书企业文化墙制作
  • 哪里有做网站设计sem模型
  • 设计素材网站有哪些免费上海微信网站建设费用
  • 湖南做网站公司专门做自助游的网站
  • 广西网站建设价格网站域名怎么解释
  • 微信网页网站怎么做网络销售是做什么
  • 网站开发与软件研发有什么区别wordpress广告管理
  • 深圳市福田建设股份有限公司网站教做缝纫的网站
  • 美食网站建设的可行性四川工程信息网官网
  • 微网站方案怎么写网站开发职业要求
  • 网站模板上传教程网站 微信认证
  • 上饶市住房和城乡建设局网站网页设计模板网站免费
  • php不用框架怎么做网站wordpress上传主题提示要ftp
  • 无锡装修网站站长工具ip查询
  • 网站提升流量it运维工程师证书
  • 商品网站策划书宁夏网站建站
  • 活动策划公司网站福州网站建设教程视频
  • 手机端网站ui做多少像素分销订单管理系统
  • 东莞大型网站建设哪家好互联网推广平台有哪些公司
  • 学建网站营销网络电话软件
  • 高密公司做网站深圳市保障性住房官网
  • 拉丝机东莞网站建设广西做网站的公司