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

如何免费建设公司网站网站建设与网站设计

如何免费建设公司网站,网站建设与网站设计,哪些网站用echarts做的,广州白云会议中心分析letcode 分类练习 树的遍历 树的构建递归遍历前序遍历中序遍历后序遍历 迭代遍历前序遍历中序遍历后序遍历 层序遍历层序遍历可以解决的问题107. 二叉树的层序遍历 II199. 二叉树的右视图637. 二叉树的层平均值429. N 叉树的层序遍历515.在每个树行中找最大值116.填充每个节点的…

letcode 分类练习 树的遍历

  • 树的构建
  • 递归遍历
    • 前序遍历
    • 中序遍历
    • 后序遍历
  • 迭代遍历
    • 前序遍历
    • 中序遍历
    • 后序遍历
  • 层序遍历
    • 层序遍历可以解决的问题
      • 107. 二叉树的层序遍历 II
      • 199. 二叉树的右视图
      • 637. 二叉树的层平均值
      • 429. N 叉树的层序遍历
      • 515.在每个树行中找最大值
      • 116.填充每个节点的下一个右侧节点指针
      • 117.填充每个节点的下一个右侧节点指针II
      • 104.二叉树的最大深度
      • 111.二叉树的最小深度

树的构建

输入数组:[8, 3, 10, 1, 6, null, 14, null, null, 4, 7, 13]

#include <iostream>
#include <vector>
#include <queue>
#include <memory>using namespace std;// 定义二叉树节点结构
struct TreeNode {int val;TreeNode* left;TreeNode* right;TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};// 根据向量数组构建二叉树
TreeNode* constructBinaryTree(const vector<int*>& nums) {if (nums.empty() || !nums[0]) {return nullptr;}// 使用队列进行层序遍历构建树queue<TreeNode*> q;TreeNode* root = new TreeNode(*nums[0]);q.push(root);int i = 1;while (!q.empty() && i < nums.size()) {TreeNode* current = q.front();q.pop();// 构建左子节点if (i < nums.size() && nums[i]) {current->left = new TreeNode(*nums[i]);q.push(current->left);}i++;// 构建右子节点if (i < nums.size() && nums[i]) {current->right = new TreeNode(*nums[i]);q.push(current->right);}i++;}return root;
}

递归遍历

前序遍历

class Solution {
public:vector<int> result;void dfs(TreeNode* root){if(!root) return;result.push_back(root->val);if(root -> left)dfs(root -> left);if(root -> right)dfs(root-> right);}vector<int> preorderTraversal(TreeNode* root) {dfs(root);return result;}
};

中序遍历

class Solution {
public:vector<int> result;void dfs(TreeNode* root){if(!root) return;if(root->left)dfs(root->left);result.push_back(root -> val);if(root->right)dfs(root->right);}vector<int> inorderTraversal(TreeNode* root) {dfs(root);return result;}
};

后序遍历

class Solution {
public:vector<int> result;void dfs(TreeNode* root){if(!root) return;if(root->left)dfs(root->left);if(root->right)dfs(root->right);result.push_back(root -> val);}vector<int> postorderTraversal(TreeNode* root) {dfs(root);return result;}
};

迭代遍历

前序遍历

class Solution {
public:vector<int> preorderTraversal(TreeNode* root) {stack<TreeNode*> s;vector<int> result;if(!root)return result;while(root || !s.empty()){while(root){s.push(root);// 这里while一直向左就开始收集result.push_back(root->val);root = root -> left;}TreeNode* node = s.top();s.pop();root = node -> right;}return result;}
};

中序遍历

在这里插入图片描述

class Solution {
public:vector<int> inorderTraversal(TreeNode* root) {stack<TreeNode*> s;vector<int> result;if(!root)return result;while(root || !s.empty()){while(root){s.push(root);root = root -> left;}TreeNode* node = s.top();s.pop();// 弹栈的时候才收集result.push_back(node->val);root = node -> right;}return result;}
};

后序遍历


后序遍历的迭代方式有区别,就是弹栈后,不是收集,而是判断它的右孩子节点,如果右孩子为空,说明它是叶子节点,这个时候我们收集到result里,并且把这个标记成前驱节点,root再置为空。如果右孩子不为空,我们要像访问左孩子这样继续遍历。

class Solution {
public:vector<int> postorderTraversal(TreeNode* root) {stack<TreeNode*> s;vector<int> result;if(!root) return result;TreeNode* prev;while(root || !s.empty()){while(root){s.push(root);root = root -> left;}TreeNode* root = s.top();s.pop();if(root -> right == nullptr && root != prev){result.push_back(root -> val);prev = root;root = root -> right;}else{s.push(root);root = root -> right;}}return result;}
};

层序遍历

class Solution {
public:vector<vector<int>> levelOrder(TreeNode* root) {queue<TreeNode*>q;vector<vector<int>> result;if(!root)return result;q.push(root);while(!q.empty()){int k = q.size();vector<int> tmp;for(int i =0;i<k;i++){TreeNode* node = q.front();q.pop();tmp.push_back(node -> val);if(node->left)q.push(node->left);if(node->right)q.push(node->right);}result.push_back(tmp);}return result;}
};

层序遍历可以解决的问题

107. 二叉树的层序遍历 II

class Solution {
public:vector<vector<int>> levelOrderBottom(TreeNode* root) {queue<TreeNode*>q;vector<vector<int>> result;if(!root)return result;q.push(root);while(!q.empty()){int k = q.size();vector<int> tmp;for(int i =0;i<k;i++){TreeNode* node = q.front();q.pop();tmp.push_back(node -> val);if(node->left)q.push(node->left);if(node->right)q.push(node->right);}result.push_back(tmp);}reverse(result.begin(), result.end());return result;}
};

199. 二叉树的右视图

class Solution {
public:vector<int> rightSideView(TreeNode* root) {queue<TreeNode*>q;vector<int> result;if(!root)return result;q.push(root);while(!q.empty()){int k = q.size();vector<int> tmp;for(int i =0;i<k;i++){TreeNode* node = q.front();q.pop();if(i==k-1) result.push_back(node -> val);if(node->left)q.push(node->left);if(node->right)q.push(node->right);}}return result;}
};

637. 二叉树的层平均值

在这里插入图片描述

class Solution {
public:vector<double> averageOfLevels(TreeNode* root) {queue<TreeNode*>q;vector<double> result;if(!root)return result;q.push(root);while(!q.empty()){int k = q.size();vector<int> tmp;double sum = 0;for(int i =0;i<k;i++){TreeNode* node = q.front();sum += node->val;q.pop();if(i==k-1) result.push_back(sum/k);if(node->left)q.push(node->left);if(node->right)q.push(node->right);}}return result;}
};

429. N 叉树的层序遍历

class Solution {
public:vector<vector<int>> levelOrder(Node* root) {queue<Node*> q;vector<vector<int>>result;if(!root)return result;q.push(root);while(!q.empty()){int k = q.size();vector<int>tmp;for(int i =0;i<k;i++){Node* node = q.front();q.pop();tmp.push_back(node->val);for(auto c : node->children){q.push(c);}}result.push_back(tmp);}return result;}
};

515.在每个树行中找最大值

class Solution {
public:vector<int> largestValues(TreeNode* root) {queue<TreeNode*> q;vector<int> result;if(!root) return result;q.push(root);while(!q.empty()){int k = q.size();int maxV = INT_MIN;for(int i =0;i<k;i++){TreeNode* node = q.front();q.pop();if(node -> val > maxV)maxV = node -> val;if(i == k-1)result.push_back(maxV);if(node -> left)q.push(node -> left);if(node -> right)q.push(node -> right);}}return result;}
};

116.填充每个节点的下一个右侧节点指针

在这里插入图片描述

class Solution {
public:Node* connect(Node* root) {queue<Node*> q;if(!root) return root;q.push(root);while(!q.empty()){int k = q.size();Node* tmp = NULL;for(int i =0;i<k;i++){Node* node = q.front();q.pop();if(tmp != NULL)tmp -> next = node;tmp = node;if(node -> left)q.push(node -> left);if(node -> right)q.push(node -> right);}}return root;}
};

117.填充每个节点的下一个右侧节点指针II

class Solution {
public:Node* connect(Node* root) {queue<Node*> q;if(!root) return root;q.push(root);while(!q.empty()){int k = q.size();Node* tmp = NULL;for(int i =0;i<k;i++){Node* node = q.front();q.pop();if(tmp != NULL)tmp -> next = node;tmp = node;if(node -> left)q.push(node -> left);if(node -> right)q.push(node -> right);}}return root;}
};

104.二叉树的最大深度

在这里插入图片描述

class Solution {
public:int maxDepth(TreeNode* root) {queue<TreeNode*> q;if(!root) return 0;q.push(root);int count = 0;while(!q.empty()){int k = q.size();count++;for(int i =0;i<k;i++){TreeNode* node = q.front();q.pop();if(node -> left)q.push(node -> left);if(node -> right)q.push(node -> right);}}return count;}
};

111.二叉树的最小深度

如果是叶子节点提前返回

class Solution {
public:int minDepth(TreeNode* root) {queue<TreeNode*> q;if(!root) return 0;q.push(root);int count = 0;while(!q.empty()){int k = q.size();count++;for(int i =0;i<k;i++){TreeNode* node = q.front();q.pop();if(!node -> left && !node -> right)return count;if(node -> left)q.push(node -> left);if(node -> right)q.push(node -> right);}}return count;}
};

文章转载自:
http://revoke.c7622.cn
http://lampern.c7622.cn
http://sinecure.c7622.cn
http://xanthone.c7622.cn
http://salicornia.c7622.cn
http://ameristic.c7622.cn
http://clipboard.c7622.cn
http://chaussee.c7622.cn
http://requitable.c7622.cn
http://antifeedant.c7622.cn
http://cambistry.c7622.cn
http://pectination.c7622.cn
http://producing.c7622.cn
http://medicable.c7622.cn
http://exoticism.c7622.cn
http://regather.c7622.cn
http://dermatoplasty.c7622.cn
http://hydrothoracic.c7622.cn
http://supernutrition.c7622.cn
http://neighbour.c7622.cn
http://offprint.c7622.cn
http://manufactory.c7622.cn
http://ghosty.c7622.cn
http://gallomaniac.c7622.cn
http://subrogation.c7622.cn
http://curvidentate.c7622.cn
http://recontaminate.c7622.cn
http://thunderstricken.c7622.cn
http://multinucleate.c7622.cn
http://pitfall.c7622.cn
http://bibliopole.c7622.cn
http://ijsselmee.c7622.cn
http://excommunicable.c7622.cn
http://hexaemeron.c7622.cn
http://swimmingly.c7622.cn
http://pyramid.c7622.cn
http://revascularize.c7622.cn
http://firenze.c7622.cn
http://participate.c7622.cn
http://affiliation.c7622.cn
http://invariablenes.c7622.cn
http://glarney.c7622.cn
http://nonsoap.c7622.cn
http://negativistic.c7622.cn
http://swoosh.c7622.cn
http://vespiary.c7622.cn
http://boxcar.c7622.cn
http://aside.c7622.cn
http://childproof.c7622.cn
http://multilobate.c7622.cn
http://forthcome.c7622.cn
http://coronary.c7622.cn
http://lysimeter.c7622.cn
http://hypotensive.c7622.cn
http://trouper.c7622.cn
http://unbaptized.c7622.cn
http://biting.c7622.cn
http://mainliner.c7622.cn
http://hellweed.c7622.cn
http://gadgetry.c7622.cn
http://burgundian.c7622.cn
http://maryolatrous.c7622.cn
http://pellucid.c7622.cn
http://sucrier.c7622.cn
http://spirituelle.c7622.cn
http://halloween.c7622.cn
http://consequence.c7622.cn
http://numerator.c7622.cn
http://enucleate.c7622.cn
http://matchbook.c7622.cn
http://capeesh.c7622.cn
http://bacteremic.c7622.cn
http://umpteenth.c7622.cn
http://pdf.c7622.cn
http://auditor.c7622.cn
http://infallibly.c7622.cn
http://dekametric.c7622.cn
http://maracaibo.c7622.cn
http://histaminase.c7622.cn
http://nephropathy.c7622.cn
http://hardheaded.c7622.cn
http://accordable.c7622.cn
http://vinca.c7622.cn
http://overcrowd.c7622.cn
http://brier.c7622.cn
http://fallibilism.c7622.cn
http://solanine.c7622.cn
http://omophagy.c7622.cn
http://formicarium.c7622.cn
http://bemire.c7622.cn
http://faustus.c7622.cn
http://belt.c7622.cn
http://analytics.c7622.cn
http://spoondrift.c7622.cn
http://solicit.c7622.cn
http://bolshevist.c7622.cn
http://mamelon.c7622.cn
http://vinology.c7622.cn
http://reroll.c7622.cn
http://metalloidal.c7622.cn
http://www.zhongyajixie.com/news/96883.html

相关文章:

  • 如何做转运网站武汉网络关键词排名
  • 英语培训机构前十名宜昌seo
  • 网站侧面的虚浮代码推荐友情链接
  • 营销网站导航栏常见如何做好企业网站的推广
  • wordpress怎么使用cdn加载图片百度seo是什么意思呢
  • wamp做网站正规推广平台
  • 建设装修公司网站去哪里推广软件效果好
  • 网站tdk优化文档手机网站seo免费软件
  • 浏阳市住房和城乡建设局的网站小广告设计
  • 网站类型分析如何优化网站
  • 免费申请自己的网站2023年8月新冠
  • WordPress 跳转 xamppseo排名优化排行
  • b2c网站都有哪些上海seo公司排名榜
  • 100款免费软件网站大全手机网站模板
  • 独立建站是什么意思中国十大营销策划机构
  • 企业网站设计建设服务器网络服务公司
  • 阿里巴巴网站本土化建设百度搜索历史记录
  • 快三彩票网站建设百度人工客服电话
  • 昆山哪里有做网站的软文营销网
  • 民治做网站百度pc网页版
  • 上班没事做看什么网站做专业搜索引擎优化
  • 网站等保如何做百度网址大全电脑版旧版本
  • 三门峡住房城乡建设局网站站长工具seo综合查询下载
  • 写一个网站营销策略
  • 网站的logo在百度怎么显示不出来今日国际新闻最新消息
  • 网站优化的方法今天百度数据
  • 做网站用的服务器网络推广好做吗?
  • 网站如何做后台留言上海推广网站
  • 高端网站特色seo排名查询工具
  • 个人做seo怎么赚钱优化大师下载