当前位置:网站首页>Binary tree traversal non-recursive program -- using stack to simulate system stack
Binary tree traversal non-recursive program -- using stack to simulate system stack
2022-08-01 00:02:00 【HUAWEI CLOUD】
一、前序遍历
给定一个二叉树,返回它的 前序 遍历.
示例:
输入: [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
边栏推荐
- 高等代数_证明_任何矩阵都相似于一个上三角矩阵
- 推荐系统:常用评价指标总结【准确率、精确率、召回率、命中率、(归一化折损累计增益)NDCG、平均倒数排名(MRR)、ROC曲线、AUC(ROC曲线下的面积)、P-R曲线、A/B测试】
- thymeleaf iterates the map collection
- 面试突击69:TCP 可靠吗?为什么?
- 【读书笔记->数据分析】02 数据分析准备
- The role of /etc/resolv.conf
- Named Entity Recognition - Model: BERT-MRC
- When can I use PushGateway
- SQL injection Less54 (limited number of SQL injection + union injection)
- C# Rectangle基本用法和图片切割
猜你喜欢
Flink 1.13(八)CDC
Interview assault 69: TCP reliable?Why is that?
游戏安全03:缓冲区溢出攻击简单解释
2022-07-31:给出一个有n个点,m条有向边的图, 你可以施展魔法,把有向边,变成无向边, 比如A到B的有向边,权重为7。施展魔法之后,A和B通过该边到达彼此的代价都是7。 求,允许施展一次魔法
C# Rectangle基本用法和图片切割
力扣二叉树
[Cloud Residency Co-Creation] [HCSD Big Celebrity Live Broadcast] Personally teach the secrets of interviews in big factories
【MATLAB项目实战】LDPC-BP信道编码
Shell常用脚本:Nexus批量上传本地仓库增强版脚本(强烈推荐)
[Reading Notes -> Data Analysis] 02 Data Analysis Preparation
随机推荐
[QNX Hypervisor 2.2用户手册]9.16 system
SQL注入 Less38(堆叠注入)
一体化步进电机在无人机自动机场的应用
vector的基本实现
什么是动态规划,什么是背包问题
Network security - crack WiFi through handshake packets (detailed tutorial)
pycaret源码分析:下载数据集\Lib\site-packages\pycaret\datasets.py
Xinao Learning Plan The Road to Informatics Competition (2022.07.31)
How to reduce the gap between software design and implementation
thymeleaf iterates the map collection
SVN server construction + SVN client + TeamCity integrated environment construction + VS2019 development
Shell常用脚本:Nexus批量上传本地仓库增强版脚本(强烈推荐)
SQL injection Less38 (stack injection)
SQL注入 Less47(报错注入) 和Less49(时间盲注)
助力数字政府建设,中科三方构建域名安全保障体系
内核对设备树的处理
2022年CSP-J1 CSP-S1 第1轮初赛 报名指南
编程语言是什么
lua入门案例实战1234定义函数与标准函数库功能
Usage of mysql having