当前位置:网站首页>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
边栏推荐
猜你喜欢

逐步手撕轮播图3(保姆级教程)

Advanced Algebra _ Proof _ Any matrix is similar to an upper triangular matrix

cobaltstrike
I don't know what to do with sync issues

虹科分享|如何用移动目标防御技术防范未知因素

【读书笔记->数据分析】02 数据分析准备

日常--Kali开启SSH(详细教程)

一行代码解决CoreData托管对象属性变更在SwiftUI中无动画效果的问题

游戏安全03:缓冲区溢出攻击简单解释

【Acwing】The 62nd Weekly Game Solution
随机推荐
Flutter教程之四年开发经验的高手给的建议
【FPGA教程案例43】图像案例3——通过verilog实现图像sobel边缘提取,通过MATLAB进行辅助验证
浏览器下载快捷方式到桌面(PWA)
继承和友元,静态成员的关系
开源好用的 流程图绘制工具 drawio
Shell常用脚本:Nexus批量上传本地仓库脚本
Usage of mysql having
一行代码解决CoreData托管对象属性变更在SwiftUI中无动画效果的问题
Interview Blitz 69: Is TCP Reliable?Why?
When can I use PushGateway
内核对设备树的处理
手写一个简单的web服务器(B/S架构)
类和对象:中
Web API 介绍和类型
命名实体识别-模型:BERT-MRC
Weekly Summary
@JsonFormat(pattern=“yyyy-MM-dd“)时间差问题
基于mysql的消息队列设计
[MATLAB project combat] LDPC-BP channel coding
Difference Between Stateless and Stateful