binary-tree-postorder-traversal leetcode C++
Given a binary tree, return the postorder traversal of its nodes' values.
For example: Given binary tree{1,#,2,3},
- 1
- \
2
- /
- 3
return[3,2,1].
Note: Recursive solution is trivial, could you do it iteratively?
C++
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode *root){
if (!root) return {};
vector<int> res;
stack<TreeNode*> s{{root}};
while(!s.empty()){
TreeNode *t = s.top(); s.pop();
res.insert(res.begin(),t->val);
if (t->left) s.push(t->left);
if (t->right) s.push(t->right);
}
return res;
}
};
binary-tree-postorder-traversal leetcode C++的更多相关文章
- Binary Tree Postorder Traversal leetcode java
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...
- Binary Tree Postorder Traversal --leetcode
原题链接:https://oj.leetcode.com/problems/binary-tree-postorder-traversal/ 题目大意:后序遍历二叉树 解题思路:后序遍历二叉树的步骤: ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
- LeetCode: Binary Tree Postorder Traversal 解题报告
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
- Binary Tree Preorder Traversal and Binary Tree Postorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
随机推荐
- python字典时间日期
字典时间日期 学习完本篇,你将会深入掌握 字典操作. 1.如何新建一个字典 2.如何正序以及反序访问字典 3.如何更新字典元素 4.如何删除字典 日期时间操作 1.如何打印一个2021-9-20 17 ...
- 测试开发【提测平台】分享10-Element UI抽屉和表单校验&增改接口合并实现应用管理
微信搜索[大奇测试开],关注这个坚持分享测试开发干货的家伙. 开篇说个小讨论,一个群里聊天聊到关于更新篇章的长度,是小篇幅多次,还是每次按照一个小完整的功能,我个人的是按照后种来的,主要的思考就是希望 ...
- 入坑微信小程序必经之路(六)图片上传服务器——WebSercice接口
wxml文件 <view class="weui-uploader"> <view class="img-v weui-uploader__bd&quo ...
- css3 显示一行内容,多余的以省略号显示
overflow: hidden; white-space: nowrap; text-overflow: ellipsis; 代码如上
- fliebeat配置手册
1. 关于Filebeat 当你要面对成百上千.甚至成千上万的服务器.虚拟机和容器生成的日志时,请告别 SSH 吧!Filebeat 将为你提供一种轻量型方法,用于转发和汇总日志与文件,让简单的事情 ...
- docker run 参数
一.格式 docker run [OPTIONS] IMAGE [COMMAND] [ARG...] 二.OPTIONS 参数 简写, 名称参数 默认参数 描述 --add-host 添加自定义主机到 ...
- GDOI2021划水记
Day0 上午有意志行,一大早就醒了,然后走了五个小时脚痛.中午洗澡,宿舍轮流看巨人最终话然后聊了一个小时? 下午老师带着我和全爷先开溜,宿舍好像很破旧还还没得充电,领了牌牌和斐爷去吃饭. 然后六点多 ...
- The art of multipropcessor programming 读书笔记-硬件基础2
本系列是 The art of multipropcessor programming 的读书笔记,在原版图书的基础上,结合 OpenJDK 11 以上的版本的代码进行理解和实现.并根据个人的查资料以 ...
- MySQL5.7主从复制-异步复制搭建
两台服务器,系统是Redhat6.5,MySQL版本是5.7.18.1.在主库上,创建复制使用的用户,并授予replication slave权限.这里创建用户repl,可以从IP为10.10.10 ...
- SimpleDateFormat、Date和String互转
今天在修改bug时遇到一个查询异常:根据时间段查询的时候,如果查询时间段含12点钟,那么能查到时间段之外的其他数据: 跟踪了数据流动发现,前同事写的程序中,有一处是讲前端传来时间字符串转为Date的一 ...