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, ...
随机推荐
- python学习09——字典(3)
今天写了一道python字典题目,用了上次字典(2)中的方法,代码如下: json = {', 'IP':'10.0.0.1'} def find_value(themap, word): if wo ...
- 转-深入理解VMware虚拟网络
原文出处:http://wangchunhai.blog.51cto.com/225186/381225 VMware Workstation是一款非常不错的虚拟机软件,许多爱好者用VMware Wo ...
- windows8.1下php环境搭建及基本配置(php+apache+mysql)
一.php下载安装:php-5.6.1-Win32-VC11-x64.zip.解压,操作: 1.复制php.ini-production,更名为php.ini 2.在环境变量PATH末尾添加:D:\p ...
- OC基础--对成员变量的封装
#import <Foundation/Foundation.h> //日期结构体 typedef struct{ int year; int month; int day; } Date ...
- 高精度快速预览打开dwg文件的CAD控件CAD Image DLL介绍及下载
CAD Image DLL对于DXF格式, DWG格式(AutoCAD R12 到AutoCAD 2004/2005), PLT 以及 HPGL/HPGL2文件都有快速的显示速度和精度,开发者再也不会 ...
- visual studio code 安装python扩展
Ctrl+P 调出控制台,在控制台里输入ext install python,点击第一个安装 如果出现: visual studio code connect ETIMEDOUT 191.238.17 ...
- Android内存优化之 LruCache与DiskLruCache
在日常的Adroid开发中我们经常遇到需要处理大量图片的地方,但Android手机的内存有限该怎么避免手机 内存溢出导致app程序oom,google提供了两种解决方式 LruCache LruCac ...
- c/c++面试题(7)零碎知识总结
1.变量的声明和定义有什么区别? 声明:变量的声明做了两件事情 a.告诉编译器这个变量已经匹配到一块内存上了,下面的代码用到的变量或对象是在别处定义的. 声明可以出现很多次. b.告诉编译器这个变量名 ...
- java获取日期 昨天 今天 明天的日期
Date date=new Date();//取时间 Calendar calendar = new GregorianCalendar(); calendar.setTime(date); cale ...
- APK签名是如何生成的
零.前言本文以支付宝手机客户端为例,进行剖析到支付宝官网下载当前最新版本:8.0.1 (2014-01-28)文件名为 alipay_wap_main.apkMD5 摘要为 69820edb3cd13 ...