当前位置:网站首页>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
边栏推荐
- Components of TypeScript
- vim的基本使用-底行模式
- How to import a Golang external package and use it?
- How to Design High Availability and High Performance Middleware - Homework
- Network security - crack WiFi through handshake packets (detailed tutorial)
- 一文概述:VPN的基本模型及业务类型
- 博弈论(Depu)与孙子兵法(42/100)
- 开源好用的 流程图绘制工具 drawio
- NIO programming
- When can I use PushGateway
猜你喜欢
随机推荐
编程语言是什么
【Acwing】第62场周赛 题解
lua入门案例实战123DIY
TFC CTF 2022 WEB Diamand WriteUp
zeno使用方法笔记
【读书笔记->数据分析】02 数据分析准备
One line of code to solve CoreData managed object properties change in SwiftUI problem of animation effects
IPD流程专业术语
精心总结十三条建议,帮你创建更合适的MySQL索引
Interview Question: Implementing Deadlocks
标段参数说明
UOS - WindTerm use
Drawing process of hand-drawn map of scenic spots
[QNX Hypervisor 2.2 User Manual]9.16 system
Kyoto University:Masaki Waga | 黑箱环境中强化学习的动态屏蔽
Xinao Learning Plan The Road to Informatics Competition (2022.07.31)
SVN server construction + SVN client + TeamCity integrated environment construction + VS2019 development
thymeleaf iterates the map collection
IPD process terminology
Redis五种数据类型简介