当前位置:网站首页>二叉树遍历非递归程序 -- 使用栈模拟系统栈
二叉树遍历非递归程序 -- 使用栈模拟系统栈
2022-07-31 23:58:00 【华为云】
一、前序遍历
给定一个二叉树,返回它的 前序 遍历。
示例:
输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3]进阶: 递归算法很简单,你可以通过迭代算法完成吗?
前序遍历C++代码
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */struct Command{ string s; // go, print TreeNode* node; Command(string s, TreeNode* node): s(s), node(node){}};class Solution {public: vector<int> preorderTraversal(TreeNode* root) { vector<int> res; if (root == NULL) { return res; } stack<Command> stack; stack.push(Command("go", root)); while ( !stack.empty() ) { Command command = stack.top(); stack.pop(); if (command.s == "print") { res.push_back(command.node -> val); } else { assert( command.s == "go"); if (command.node -> right) { stack.push(Command("go", command.node -> right)); } if (command.node -> left) { stack.push(Command("go", command.node -> left)); } stack.push(Command("print", command.node)); } } return res; }};二、中序遍历
给定一个二叉树,返回它的中序 遍历。
示例:
输入: [1,null,2,3] 1 \ 2 / 3输出: [1,3,2]进阶: 递归算法很简单,你可以通过迭代算法完成吗?
中序遍历C++代码
// @lc code=start/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */struct Command{ string s; // go, print TreeNode *node; Command(string s, TreeNode *node) : s(s), node(node) {}};class Solution{public: vector<int> inorderTraversal(TreeNode *root) { vector<int> res; if (root == NULL) { return res; } stack<Command> stack; stack.push(Command("go", root)); while (!stack.empty()) { Command command = stack.top(); stack.pop(); if (command.s == "print") { res.push_back(command.node->val); } else { assert(command.s == "go"); if (command.node->right) { stack.push(Command("go", command.node->right)); } stack.push(Command("print", command.node)); if (command.node->left) { stack.push(Command("go", command.node->left)); } } } return res; }};// @lc code=end三、后序遍历
给定一个二叉树,返回它的 后序 遍历。
示例:
输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1]进阶: 递归算法很简单,你可以通过迭代算法完成吗?
后序遍历C++代码
// @lc code=start/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */struct Command{ string s; // go, print TreeNode *node; Command(string s, TreeNode *node) : s(s), node(node) {}};class Solution{public: vector<int> postorderTraversal(TreeNode *root) { vector<int> res; if (root == NULL) { return res; } stack<Command> stack; stack.push(Command("go", root)); while (!stack.empty()) { Command command = stack.top(); stack.pop(); if (command.s == "print") { res.push_back(command.node->val); } else { assert(command.s == "go"); stack.push(Command("print", command.node)); if (command.node->right) { stack.push(Command("go", command.node->right)); } if (command.node->left) { stack.push(Command("go", command.node->left)); } } } return res; }};// @lc code=end练习题: 341.Flatten Nested List Iterator
边栏推荐
- vector的基本实现
- Components of TypeScript
- 一体化步进电机在无人机自动机场的应用
- 力扣2326、197
- [Cloud Residency Co-Creation] [HCSD Big Celebrity Live Broadcast] Personally teach the secrets of interviews in big factories
- Mysql environment installation under Linux (centos)
- Flutter教程之 02 Flutter 桌面程序开发入门教程运行hello world (教程含源码)
- Interview Blitz 69: Is TCP Reliable?Why?
- SQL注入 Less42(POST型堆叠注入)
- 【MATLAB项目实战】LDPC-BP信道编码
猜你喜欢

推荐系统:常用评价指标总结【准确率、精确率、召回率、命中率、(归一化折损累计增益)NDCG、平均倒数排名(MRR)、ROC曲线、AUC(ROC曲线下的面积)、P-R曲线、A/B测试】

Payment module implementation

【读书笔记->数据分析】02 数据分析准备

硬件设备计算存储及数据交互杂谈

一体化步进电机在无人机自动机场的应用

力扣二叉树

《ArchSummit:时代的呐喊,技术人听得到》

MLP神经网络,GRNN神经网络,SVM神经网络以及深度学习神经网络对比识别人体健康非健康数据

一行代码解决CoreData托管对象属性变更在SwiftUI中无动画效果的问题

One line of code to solve CoreData managed object properties change in SwiftUI problem of animation effects
随机推荐
Pytest first experience
【ACM】2022.7.31训练赛
Named Entity Recognition - Model: BERT-MRC
[MATLAB project combat] LDPC-BP channel coding
Program processes and threads (concurrency and parallelism of threads) and basic creation and use of threads
SQL injection Less47 (error injection) and Less49 (time blind injection)
【1161. 最大层内元素和】
IPD process terminology
信奥学习规划 信息学竞赛之路(2022.07.31)
(26)Blender源码分析之顶层菜单的关于菜单
Flutter教程之 01配置环境并运行demo程序 (教程含源码)
网易云信圈组上线实时互动频道,「破冰」弱关系社交
开源好用的 流程图绘制工具 drawio
SQL注入 Less47(报错注入) 和Less49(时间盲注)
The difference between adding or not adding the ref keyword when a variable of reference type is used as a parameter in a method call in C#
Shell常用脚本:Nexus批量上传本地仓库增强版脚本(强烈推荐)
基于simulink的Passive anti-islanding-UVP/OVP and UFP/OFP被动反孤岛模型仿真
助力数字政府建设,中科三方构建域名安全保障体系
EntityFramework保存到SQLServer 小数精度丢失
/etc/sysconfig/network-scripts configure the network card