当前位置:网站首页>二叉树遍历非递归程序 -- 使用栈模拟系统栈
二叉树遍历非递归程序 -- 使用栈模拟系统栈
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
边栏推荐
- [QNX Hypervisor 2.2用户手册]9.16 system
- vector的基本实现
- Flink 1.13(八)CDC
- MLP神经网络,GRNN神经网络,SVM神经网络以及深度学习神经网络对比识别人体健康非健康数据
- Flutter教程之 02 Flutter 桌面程序开发入门教程运行hello world (教程含源码)
- 游戏安全03:缓冲区溢出攻击简单解释
- 逐步手撕轮播图3(保姆级教程)
- 高等代数_证明_任何矩阵都相似于一个上三角矩阵
- 【FPGA教程案例43】图像案例3——通过verilog实现图像sobel边缘提取,通过MATLAB进行辅助验证
- [MATLAB project combat] LDPC-BP channel coding
猜你喜欢
Flink 1.13(八)CDC
程序进程和线程(线程的并发与并行)以及线程的基本创建和使用
IJCAI2022 | 代数和逻辑约束的混合概率推理
基于simulink的Passive anti-islanding-UVP/OVP and UFP/OFP被动反孤岛模型仿真
如何设计高可用高性能中间件 - 作业
One line of code to solve CoreData managed object properties change in SwiftUI problem of animation effects
2022年最新重庆建筑八大员(电气施工员)模拟题库及答案
Binary tree non-recursive traversal
嵌入式开发没有激情了,正常吗?
NIO编程
随机推荐
Drawing process of hand-drawn map of scenic spots
TFC CTF 2022 WEB Diamand WriteUp
【ACM】2022.7.31训练赛
浏览器下载快捷方式到桌面(PWA)
SQL注入 Less46(order by后的注入+rand()布尔盲注)
类和对象:上
一行代码解决CoreData托管对象属性变更在SwiftUI中无动画效果的问题
Daily--Kali opens SSH (detailed tutorial)
TFC CTF 2022 WEB Diamand WriteUp
无状态与有状态的区别
什么是客户画像管理?
一文概述:VPN的基本模型及业务类型
SVN server construction + SVN client + TeamCity integrated environment construction + VS2019 development
date命令
Binary tree non-recursive traversal
I don't know what to do with sync issues
对象缓存服务的思考和实现
"SDOI2016" Journey Problem Solution
2022-07-31:给出一个有n个点,m条有向边的图, 你可以施展魔法,把有向边,变成无向边, 比如A到B的有向边,权重为7。施展魔法之后,A和B通过该边到达彼此的代价都是7。 求,允许施展一次魔法
Keil nRF52832下载失败