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

做网站的软件叫什么网页入口网站推广

做网站的软件叫什么,网页入口网站推广,赚钱软件微信提现,潍坊市建设局门户网站Problem: 199. 二叉树的右视图 文章目录 题目描述思路解题方法复杂度Code 题目描述 思路 无论是DFS还是BFS我们都要思考到达二叉树的每一层(或者每一层中的每一个节点)时,我们都该如何按题目要求做出对应得处理!!!在本体中我们主要是&#x…

Problem: 199. 二叉树的右视图

文章目录

  • 题目描述
  • 思路
  • 解题方法
  • 复杂度
  • Code

题目描述

在这里插入图片描述在这里插入图片描述

思路

无论是DFS还是BFS我们都要思考到达二叉树的每一层(或者每一层中的每一个节点)时,我们都该如何按题目要求做出对应得处理!!!在本体中我们主要是:

1.当右子树与左子树等高或者右子树高于左子树时,我们只添加每一个右子树得右节点到结果集(根节点得左子树整个都去除)
2.当左子树高于右子树时,我们将等高得部分按1中处理,左子树高出右子树得部分,再将其右子树得右节点添加到结果集

解题方法

思路1:DFS

1.创建unordered_map<int, int> rightmostValueAtDepth;记录每一层应该添加到右视图的节点,int maxDepth = -1;记录并维护当前的最大深度,stack<TreeNode > nodeStack;用于DFS过程中的存储节点,stack depthStack;用于同时记录DFS过程中的树的深度;
2.将根节点添加到nodeStack中,while循环遍历(循环退出条件为nodeStack为空),每次弹出nodeStack和depthStack中的栈顶元素
node
depth
,若此时
node不为空
则更新最大深度,同时
若rightmostValueAtDepth[depth]不存在则添加到rightmostValueAtDepth中*;并且将node -> left;node -> right;depth + 1;depth + 1分别添加到对应的nodeStack和depthStack栈
3.最后将rightmostValueAtDepth中的值添加到一个一维数组中即可

思路2:BFS
大体实现直接套用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 vector<int>*/vector<int> rightSideView(TreeNode *root) {if (root == nullptr) {return {};}unordered_map<int, int> rightmostValueAtDepth;int maxDepth = -1;stack<TreeNode *> nodeStack;stack<int> 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);}}vector<int> 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 vector<int>*/vector<int> rightSideView(TreeNode* root) {if (root == nullptr) {return {};}if (root -> left == nullptr && root -> right == nullptr) {return {root -> val};}vector<int> res;vector<int> temp(1);queue<TreeNode*> 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.ritt.cn/news/22385.html

相关文章:

  • 手机app 网站常见的网络营销模式
  • erp企业生产管理系统快速优化工具
  • 盐城做企业网站公司海淀区seo引擎优化多少钱
  • 安阳信息港网站ip营销的概念
  • 搭建本地网页深圳百度推广seo公司
  • 新网站上线怎么做seogoogle官网入口手机版
  • 做自己的网站网站推广软件
  • 浅析个人网站的设计论文学生制作个人网站
  • 闸北网站建设免费b站推广网站2023
  • 代刷网站只做软件吗简易的旅游网页制作
  • 做网站注册什么性质的公司小程序平台
  • angularjs 网站模板怎么查百度收录
  • 家乐福网上商城客服网络营销推广及优化方案
  • 做网站的前端技术优化营商环境工作开展情况汇报
  • 网站建设 域名 管理品牌关键词排名优化怎么做
  • 怎么做网站推广林芝地区优化推广联盟
  • 泊头做网站找哪家好大数据营销软件
  • 做竞价网站需要什么样的空间湖南网站制作哪家好
  • 鞍山网站制作长尾词挖掘工具
  • 网站注册费seo怎么快速提高排名
  • 如何设计网站的链接上海怎么做seo推广
  • 昆山自适应网站建设专注网站建设服务机构
  • 做网站大概需要几个人流程优化四个方法
  • 用什么做网站最好推广途径有哪些
  • 宝安营销型网站建设公司淘宝推广平台有哪些
  • 服务哪家好中医小程序定制seo单词优化
  • 个性化网站定制价格北京公司排名seo
  • 我的网站不做推广 百度能搜索到我网站吗大学生网络营销策划方案书
  • 重庆企业网站推广流程抖音搜索seo排名优化
  • 如何做一个二维码相册seo最新快速排名