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

一文概述:VPN的基本模型及业务类型

C# Rectangle basic usage and picture cutting
Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team

【云驻共创】【HCSD大咖直播】亲授大厂面试秘诀

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

NIO编程

什么是动态规划,什么是背包问题

2022-07-31:给出一个有n个点,m条有向边的图, 你可以施展魔法,把有向边,变成无向边, 比如A到B的有向边,权重为7。施展魔法之后,A和B通过该边到达彼此的代价都是7。 求,允许施展一次魔法

What is customer profile management?

Shell common script: Nexus batch upload local warehouse script
随机推荐
内核对设备树的处理
精心总结十三条建议,帮你创建更合适的MySQL索引
ICML2022 | 深入研究置换敏感的图神经网络
NIO编程
What is customer profile management?
leetcode:126. 单词接龙 II
继承的注意事项
/etc/sysconfig/network-scripts 配置网卡
南方科技大学:Xiaoying Tang | AADG:视网膜图像分割领域泛化的自动增强
Compose原理-视图和数据双向绑定的原理
虚继承的原理
Google Earth Engine——Error: Image.clipToBoundsAndScale, argument ‘input‘: Invalid type的错误解决
lua入门案例实战1234定义函数与标准函数库功能
Shell常用脚本:Nexus批量上传本地仓库增强版脚本(强烈推荐)
IPD process terminology
【读书笔记->数据分析】02 数据分析准备
Keil nRF52832下载失败
【FPGA教程案例43】图像案例3——通过verilog实现图像sobel边缘提取,通过MATLAB进行辅助验证
周总结
《ArchSummit:时代的呐喊,技术人听得到》