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

代码随想录:二叉树的四种遍历

144. 二叉树的前序遍历

/*** 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> preorderTraversal(TreeNode* root) {vector<int> anwser;traval(root,anwser);return anwser;}void traval(TreeNode* root,vector<int>& anwser){if(!root)return ;anwser.push_back(root->val);traval(root->left,anwser);traval(root->right,anwser);return ;}
};

145. 二叉树的后序遍历

/*** 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> postorderTraversal(TreeNode* root) {vector<int> anwser;traval(root,anwser);return anwser;}void traval(TreeNode *root,vector<int>& anwser){if(root==NULL)return ;traval(root->left,anwser);traval(root->right,anwser);anwser.push_back(root->val);return;}
};

94. 二叉树的中序遍历

/*** 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> inorderTraversal(TreeNode* root) {vector<int>anwser;traval(root,anwser);return anwser;}void traval(TreeNode *root,vector<int> & anwser){if(root==NULL)return;traval(root->left,anwser);anwser.push_back(root->val);traval(root->right,anwser);return;}
};

102. 二叉树的层序遍历

/*** 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>> levelOrder(TreeNode* root) {vector<vector<int>> anwser;queue<TreeNode*> q;if(root)q.push(root);while(!q.empty()){int n=q.size();vector<int>layer;TreeNode* cur=q.front();for(int i=0;i<n;i++){TreeNode* cur=q.front();q.pop();if(cur->left)q.push(cur->left);if(cur->right)q.push(cur->right);layer.push_back(cur->val);}anwser.push_back(layer);}return anwser;}
};
http://www.lryc.cn/news/464711.html

相关文章:

  • 【Linux】从多线程同步到生产者消费者模型:多线程编程实践
  • 如何在word里面给文字加拼音?
  • Detr论文精读
  • 找寻孤独伤感视频素材的热门资源网站推荐
  • 大模型~合集13
  • 【Next.js 项目实战系列】04-修改 Issue
  • 【Linux】并行与并发(含时间片)
  • 【Flutter】页面布局:弹性布局(Flex)
  • 深入解析 Go 语言接口:多接口实现与接口组合的实际应用
  • Eclipse——Java开发详解
  • 练手小项目推荐
  • 一图秒懂色彩空间和色彩模型
  • 控制Stable Diffusion生成质量的多种方法
  • 递归算法笔记
  • Android——发送彩信
  • 对比迁移项目的改动
  • 数据结构-复杂度
  • 无人机之放电速率篇
  • 免费开源AI助手,颠覆你的数字生活体验
  • VMware虚拟机三种网络模式详解
  • 【算法篇】动态规划类(4)——子序列(笔记)
  • 【图解版】力扣第162题:寻找峰值
  • Windows电脑桌面如何弄个好用的提醒备忘录?
  • Windows API 一 ----起步
  • 音视频入门基础:H.264专题(19)——FFmpeg源码中,获取avcC封装的H.264码流中每个NALU的长度的实现
  • 【uniapp】设置公共样式,实现公共背景等
  • Node.js学习笔记
  • resnetv1骨干
  • 设计模式,面试级别的详解(持续更新中)
  • 第9篇:网络访问控制与认证机制