当前位置:网站首页>二叉树遍历非递归程序 -- 使用栈模拟系统栈
二叉树遍历非递归程序 -- 使用栈模拟系统栈
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
边栏推荐
- 内核对设备树的处理
- Interview assault 69: TCP reliable?Why is that?
- Handwritten a simple web server (B/S architecture)
- 如何导入 Golang 外部包并使用它?
- vim的基本使用概念
- IPD流程专业术语
- MySQL数据库‘反斜杠\’ ,‘单引号‘’,‘双引号“’,‘null’无法存储
- Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
- Advanced Algebra _ Proof _ Any matrix is similar to an upper triangular matrix
- 不知道该怎么办的同步问题
猜你喜欢
随机推荐
日常--Kali开启SSH(详细教程)
SQL27 View user details of different age groups
如何导入 Golang 外部包并使用它?
SQL injection Less54 (limited number of SQL injection + union injection)
UOS统信系统 - WindTerm使用
[QNX Hypervisor 2.2 User Manual]9.16 system
Flutter教程之 01配置环境并运行demo程序 (教程含源码)
NIO编程
基于simulink的Active anti-islanding-AFD主动反孤岛模型仿真
[MATLAB project combat] LDPC-BP channel coding
UOS - WindTerm use
ICML2022 | 深入研究置换敏感的图神经网络
IJCAI2022 | 代数和逻辑约束的混合概率推理
SQL注入 Less47(报错注入) 和Less49(时间盲注)
TFC CTF 2022 WEB Diamand WriteUp
Program processes and threads (concurrency and parallelism of threads) and basic creation and use of threads
The difference between /usr/local/bin and /usr/bin
一行代码解决CoreData托管对象属性变更在SwiftUI中无动画效果的问题
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#
How to Design High Availability and High Performance Middleware - Homework