leetcode6:binary-tree-postorder-traversal
题目描述
1↵ ↵ 2↵ /↵ 3↵
Given binary tree{1,#,2,3},
1↵ ↵ 2↵ /↵ 3↵
return[3,2,1].
Note: Recursive solution is trivial, could you do it iteratively?
输出
[3,2,1]
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类
* @return int整型vector
*/
vector<int> postorderTraversal(TreeNode* root) {
// write code here
vector <int> res;
if (!root)
return res;
stack<TreeNode *> st;
st.push(root);
while (st.size())
{
TreeNode *temp=st.top();
st.pop();
res.push_back(temp->val);
if (temp->left)
st.push(temp->left);
if (temp->right)
st.push(temp->right);
}
reverse (res.begin(),res.end());
return res;
}
};
leetcode6:binary-tree-postorder-traversal的更多相关文章
- 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 ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- 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 ...
- 二叉树前序、中序、后序非递归遍历 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 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- 145. Binary Tree Postorder Traversal
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
随机推荐
- DX12龙书 00 - 环境配置:通过 Visual Studio 2019 运行示例项目
0x00 安装 Visual Studio 2019 安装 Visual Studio 2019 以及相关组件. 注:安装组件时带的 Windows 10 SDK 可以在 Individual com ...
- Request对象基础应用实例代码一
输入用户名:<br><input type="text" name="yhm"><br><br>输入密码:< ...
- 多测师讲解pthon_002字符,列表,元组,字段等
# # # 索引:# # # 正向索引: 0 1 2 3 4 5 6# # # l= a b c d e f g# # # 反向索引: -7 -6 -5 -4 ...
- 彻底根治window弹窗小广告(今日热点)
在一个阴雨蒙蒙的下午,我上完厕所回到工位,输入锁屏密码,解锁,蹦出来三个小广告,我......这还能忍??? 废话不多说,开搞! 一.广告分为两种: 红色字的今日热点 蓝色字的今日热点 二.追溯根源: ...
- JAVA Schedule的Cron表达式
spring中用到的定时任务,一般用到的有Timer()和Schedule Cron表达式一般是程序的定时任务中所要起的..我们用的springboot中的@Schedule中,启动类中添加enabl ...
- 模块二:ES新特性与TypeScript、JS性能优化
一.请说出下列最终得执行结果,并解释为什么.
- Docker学习笔记之-通过Xshell连接 CentOS服务
上一节演示如何在虚拟机中安装 CentOS服务,Docker学习笔记之-在虚拟机VM上安装CentOS 7.8 本节主要演示如何通过 Xshell软件链接CentOS服务,本例以虚拟机作为演示,直接在 ...
- centos 7安装搜狗输入法之失败案例
最近打算在旧电脑上安装centos用,先用虚拟机做个测试 默认的intelligence pinyin不太好用,打算安装搜狗输入法.在网上找了几篇看起来还"不错"的, 基本上来第一 ...
- Java 等待/通知机制
等待/通知的目的是确保等待线程从wait()方法返回时能够感知到通知线程对变量所做出的的修改: 等待方遵循如下原则: 1.获取对象的锁 2.如果条件不满足,那么调用对象的wait()方法,被通知后任要 ...
- git学习(五) git diff操作
git diff操作 git diff用于比较差异: git diff 不加任何参数 用于比较当前工作区跟暂存区的差异 git diff --cached 或者--staged 对比暂存区(git a ...