当前位置:网站首页>二叉树遍历非递归程序 -- 使用栈模拟系统栈
二叉树遍历非递归程序 -- 使用栈模拟系统栈
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
边栏推荐
- SQL injection Less42 (POST type stack injection)
- 面试突击69:TCP 可靠吗?为什么?
- 推荐系统:常用评价指标总结【准确率、精确率、召回率、命中率、(归一化折损累计增益)NDCG、平均倒数排名(MRR)、ROC曲线、AUC(ROC曲线下的面积)、P-R曲线、A/B测试】
- 周总结
- Network security - crack WiFi through handshake packets (detailed tutorial)
- SQL注入 Less42(POST型堆叠注入)
- SQL注入 Less54(限制次数的SQL注入+union注入)
- Flutter教程之 01配置环境并运行demo程序 (教程含源码)
- cobaltstrike
- Pytest first experience
猜你喜欢

基于simulink的Active anti-islanding-AFD主动反孤岛模型仿真

One line of code to solve CoreData managed object properties change in SwiftUI problem of animation effects

一文带你了解 Grafana 最新开源项目 Mimir 的前世今生

浏览器下载快捷方式到桌面(PWA)

基于单片机GSM的防火防盗系统的设计

Payment module implementation

Daily--Kali opens SSH (detailed tutorial)

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#

编译型语言和解释型语言的区别

Interview assault 69: TCP reliable?Why is that?
随机推荐
Handwritten a simple web server (B/S architecture)
EntityFramework保存到SQLServer 小数精度丢失
消息队列存储消息数据的MySQL表格
Fixed-length usage of nanopb string type based on RT1052 Aworks (27)
Shell common script: Nexus batch upload local warehouse script
VOT2021 game introduction
【云驻共创】【HCSD大咖直播】亲授大厂面试秘诀
助力数字政府建设,中科三方构建域名安全保障体系
面试突击69:TCP 可靠吗?为什么?
虚继承的原理
Shell常用脚本:Nexus批量上传本地仓库增强版脚本(强烈推荐)
(26) About menu of the top menu of Blender source code analysis
Named Entity Recognition - Model: BERT-MRC
IJCAI2022 | 代数和逻辑约束的混合概率推理
@JsonFormat(pattern=“yyyy-MM-dd“)时间差问题
面试突击69:TCP 可靠吗?为什么?
Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
Interview Blitz 69: Is TCP Reliable?Why?
NgRx 里 first 和 take(1) 操作符的区别
One line of code to solve CoreData managed object properties change in SwiftUI problem of animation effects