Binary Tree Postorder Traversal
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]
.
/**
* Definition for a binary tree node.
* 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) {
vector<int> res;
help(root,res);
return res;
}
void help(TreeNode *root,vector<int> &res)
{
if(root!=NULL)
{
help(root->left,res);
help(root->right,res);
res.push_back(root->val);
}
}
};
class Solution {
public:
vector<int> postorderTraversal(TreeNode *root)
{
vector<int> res;
stack<pair<TreeNode*,int>> s;
s.push(make_pair(root,));
while(!s.empty())
{
TreeNode *now=s.top().first;
if(now==NULL)
s.pop();
else
{
switch(s.top().second++)
{
case :
s.push(make_pair(now->left,));
break;
case :
s.push(make_pair(now->right,));
break;
default:
s.pop(); res.push_back(now->val);
break;
}
}
}
return res;
}
};
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, ...
随机推荐
- mysql连接查询和子查询
一.连接查询 1.交叉连接 就是从一张表的一条记录去连接另一张表中的所有记录,并且保存所有的记录,其中包括两个表的所有的字段! 从结果上看,就是对两张表做笛卡尔积! 笛卡尔积也就是两个表中所有可能的连 ...
- 数组求和,计算给定数组 arr 中所有元素的总和
一,题目分析:可以使用数组的归并方法计算,reduce和reduceRight.二者作用几乎相同.只是归并方向相反.reduce和reduceRight都可以接收两个参数.第一个是在每一项上调用的函数 ...
- Spring学习笔记
Spring 的控制反转:把对象的创建.初始化.销毁等工作交给Spring 容器来做,有spring容器控制对象的生命周期 applicationContext.xml beans --->sp ...
- 手把手教你在Ubuntu上安装Apache、MySql和PHP
1:首先安装apache:打开终端(ctrl+Alt+t), 输入命令:sudo apt-get install apache2即可安装, 安装完后,打开浏览器,在地址栏输入:localhost或者h ...
- 一个有趣的回答(摘自http://www.51testing.com/html/03/n-860703.html)
假设这有一个各种字母组成的字符串,假设这还有另外一个字符串,而且这个字符串里的字母数相对少一些.从算法上讲,什么方法能最快的查出所有小字符串里的字母在大字符串里都有? 比如,如果是下面两个字符串: S ...
- js 控制表单提交
<form id="form2"> <input type="text" name="text" value=" ...
- linux命令每日一练习 创建新文件 列出文件的时候带着行号
touch ××× nl ****
- CONTAINING_RECORD的实现
// 9.25.cpp : 定义控制台应用程序的入口点.///*----------------------------CONTAINING_RECODER的实现------------------- ...
- VMware下利用ubuntu13.04建立嵌入式开发环境之五
tftp和nfs服务器配置 一.tftp服务器配置 1.安装软件包 1.1安装服务 apt-get install tftpd-hpa 1.2安装客户端 apt-get install tftp-hp ...
- c/c++面试题(7)零碎知识总结
1.变量的声明和定义有什么区别? 声明:变量的声明做了两件事情 a.告诉编译器这个变量已经匹配到一块内存上了,下面的代码用到的变量或对象是在别处定义的. 声明可以出现很多次. b.告诉编译器这个变量名 ...