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

二叉树的遍历

森林二叉树
先序遍历先序遍历先序遍历
序遍历中序遍历中序遍历

1.前序遍历

leetcode题目链接

1.1 递归

前序遍历递归方式

class Solution {
public:vector<int> preorderTraversal(TreeNode* root) {vector<int> res;if(root){res.push_back(root->val);vector<int> l = preorderTraversal(root->left);res.insert(res.end(),l.begin(),l.end());vector<int> r = preorderTraversal(root->right);res.insert(res.end(),r.begin(),r.end());}return res;}
};

1.2 非递归

前序遍历迭代方式一

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

前序遍历迭代方式二

class Solution {
public:vector<int> preorderTraversal(TreeNode* root) {vector<int> res;stack<TreeNode*> s;while( root || s.size()){while(root){res.push_back(root->val);s.push(root);root = root->left;}root = s.top() , s.pop();root = root->right;}return res;}
};

2 中序遍历

leetcode题目链接

2.1 递归

中序遍历递归方式

class Solution {
public:vector<int> inorderTraversal(TreeNode* root) {vector<int> res;if(root){vector<int> l = inorderTraversal(root->left);res.insert(res.end(),l.begin(),l.end());res.push_back(root->val);vector<int> r = inorderTraversal(root->right);res.insert(res.end(),r.begin(),r.end());}return res;}
};

非递归

中序遍历迭代方式一

class Solution {
public:vector<int> inorderTraversal(TreeNode* root) {vector<int> res;stack<TreeNode*> s;while(root || s.size()){if( root ){s.push(root);root = root->left;}else{root = s.top() , s.pop();res.push_back(root->val);root = root->right;}}return res;}
};

中序遍历迭代方式二

class Solution {
public:vector<int> inorderTraversal(TreeNode* root) {vector<int> res;stack<TreeNode*> s;while(root || !s.empty()){while(root){s.push(root);root = root->left;}root = s.top() , s.pop();res.push_back(root->val);root = root->right;}return res;}
};

3 后序遍历

leetcode题目链接

3.1 递归

后序递归遍历方式

class Solution {
public:vector<int> postorderTraversal(TreeNode* root) {vector<int> res;if(root){vector<int> l = postorderTraversal(root->left);res.insert(res.end(),l.begin(),l.end());vector<int> r = postorderTraversal(root->right);res.insert(res.end(),r.begin(),r.end());res.push_back(root->val);}return res;}
};

3.2 非递归

后序遍历迭代方式

class Solution {
public:vector<int> postorderTraversal(TreeNode* root) {vector<int> res;stack<TreeNode*> s;TreeNode* pre = NULL;while(root || s.size()){if(root){s.push(root);root = root->left;}else{root = s.top();if(root->right && pre != root->right)root = root->right;else{s.pop();res.push_back(root->val);pre = root;root = NULL;}}}return res;}
};
http://www.lryc.cn/news/210928.html

相关文章:

  • 分布式限流:Redis
  • python excel接口自动化测试框架
  • Java开发面试--MongoDB专区
  • 当『后设学习』碰上『工程学思维』
  • 一表谈现实、系统、流程、报表与BI
  • 数据结构顺序栈例题一
  • 大模型在百度智能问答、搜索中的应用
  • ARPG----C++学习记录01日志和调试
  • 3302. 表达式求值, 栈的应用
  • 论文写作框架示例:论软件系统建模方法及其应用
  • Godot 官方2D C#重构(4):TileMap进阶使用
  • Ubuntu系统编译调试QGIS源码保姆级教程
  • 电源控制系统架构(PCSA)之系统控制处理器
  • 深度学习之轻量级神经网络在TWS蓝牙音频处理器上的部署
  • Redis实现方式开启新篇章,解决分布式环境下的资源竞争问题,提升系统稳定性
  • Go命令行参数操作:os.Args、flag包
  • 在Go中处理时间数据
  • SOLIDWORKS PDM 2024数据管理5大新功能
  • 5G与医疗:开启医疗技术的新篇章
  • Linux云服务器限制ip进行ssh远程连接
  • 【100天精通Python】Day72:Python可视化_一文掌握Seaborn库的使用《二》_分类数据可视化,线性模型和参数拟合的可视化,示例+代码
  • (二开)Flink 修改源码拓展 SQL 语法
  • java中spi与api的区别
  • 【Android知识笔记】插件化专题(二)
  • 赶紧收藏!史上最全IDEA快捷键大全
  • IntelliJ IDEA 把package包展开和压缩
  • Python——自动创建文件夹
  • Leetcode—21.合并两个有序链表【简单】
  • 数据链路层和DNS之间的那些事~
  • Spring-声明式事务