当前位置:网站首页>二叉树遍历非递归程序 -- 使用栈模拟系统栈
二叉树遍历非递归程序 -- 使用栈模拟系统栈
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 Less38 (stack injection)
- Interview assault 69: TCP reliable?Why is that?
- SQL injection Less54 (limited number of SQL injection + union injection)
- MLP神经网络,GRNN神经网络,SVM神经网络以及深度学习神经网络对比识别人体健康非健康数据
- 面试突击69:TCP 可靠吗?为什么?
- I don't know what to do with sync issues
- 不知道该怎么办的同步问题
- cobaltstrike
猜你喜欢
Program processes and threads (concurrency and parallelism of threads) and basic creation and use of threads
SVN服务器搭建+SVN客户端+TeamCity集成环境搭建+VS2019开发
硬件设备计算存储及数据交互杂谈
Network security - crack WiFi through handshake packets (detailed tutorial)
SVN server construction + SVN client + TeamCity integrated environment construction + VS2019 development
Recommendation system: Summary of common evaluation indicators [accuracy rate, precision rate, recall rate, hit rate, (normalized depreciation cumulative gain) NDCG, mean reciprocal ranking (MRR), ROC
C# Rectangle basic usage and picture cutting
手写一个简单的web服务器(B/S架构)
【读书笔记->数据分析】02 数据分析准备
/etc/sysconfig/network-scripts 配置网卡
随机推荐
NIO编程
PHP三元(三目)运算符
lua入门案例实战123DIY
I don't know what to do with sync issues
Pytest first experience
How to Design High Availability and High Performance Middleware - Homework
高等代数_证明_任何矩阵都相似于一个上三角矩阵
浏览器下载快捷方式到桌面(PWA)
Input and output optimization
Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
date命令
SQL injection Less47 (error injection) and Less49 (time blind injection)
SQL injection Less46 (injection after order by + rand() Boolean blind injection)
"SDOI2016" Journey Problem Solution
Fixed-length usage of nanopb string type based on RT1052 Aworks (27)
数据分析(一)——matplotlib
leetcode:126. 单词接龙 II
"APIO2010" Patrol Problem Solution
C# Rectangle基本用法和图片切割
TFC CTF 2022 WEB Diamand WriteUp