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].

思路:后序遍历是按照“左子树,右子树,根”的顺序访问元素。那么根或者其它父亲元素就要先压入栈,然后再弹出。

#include <iostream>
#include <algorithm>
#include <vector>
#include <stack> using namespace std; 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;
stack<TreeNode *> s;
if (!root) {
return res;
}
s.push(root);
while (!s.empty()) {
TreeNode *p = s.top(); s.pop();
res.push_back(p->val); if (p->right) {
s.push(p->right);
} if (p->left) {
s.push(p->left);
}
}
reverse(res.begin(), res.end());
return res;
}
}; int main(int argc, char *argv[]) {
TreeNode *p = new TreeNode();
p->right = new TreeNode();
p->left = new TreeNode(); Solution *solution = new Solution(); vector<int> res;
res = solution->postorderTraversal(p); vector<int>::iterator it;
for (it = res.begin(); it != res.end(); it++) {
cout << *it << endl;
} }

leetcode - [6]Binary Tree Postorder Traversal的更多相关文章

  1. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

  2. C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)

    145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...

  3. LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

    翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...

  4. [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  5. Java for LeetCode 145 Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  6. leetcode 145. Binary Tree Postorder Traversal ----- java

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  7. leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

    题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...

  8. LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...

  9. 【leetcode】Binary Tree Postorder Traversal

    题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...

随机推荐

  1. go语言中操作mysql的方法

    需要下载指定的golang的mysql驱动包 > go get github.com/go-sql-driver/mysql 下面的例子: package main; import ( &quo ...

  2. 为什么使用Reazor

    原因:类似于前边写的模板页,自己写了.还需要用replace来替换成自己想要的变量.. 常见的模板引擎:Razor.Nvelocity.Vtemplate. Razor有VS自动提示,而且有助于学习a ...

  3. openshift上传java web项目

    下载当前客户端 OC(Openshift Client) https://mirror.openshift.com/pub/openshift-v3/clients/3.9.14/windows/oc ...

  4. (转)android拨打电话崩溃6.0以上实时动态权限申请

    文章转自:http://blog.csdn.net/qq_29988575/article/details/54909213 6.0以下手机正常,6.0以上的却崩溃 解决方法: targetSdkVe ...

  5. HTML与XHTML的差别(转自)http://jingyan.baidu.com/article/597035521c31ed8fc007400a.html

    HTML与XHTML之间的差别,粗略可以分为两大类比较:一个是功能上的差别,另外是书写习惯的差别.关于功能上的差别,主要是XHTML可兼容各大浏览器.手机以及PDA,并且浏览器也能快速正确地编译网页. ...

  6. javascript 高级程序设计 五

    1.变量: ECMAScript中的基本类型都是值类型Boolean,Number,Null,Undefined和String,在这里JS和其他的语言有所不同,就是JS中的String是值类型 而不像 ...

  7. HISAT2,StringTie,Ballgown处理转录组数据

    HISAT2,StringTie,Ballgown处理转录组数据 本文总阅读量次2017-05-26 HISAT2,StringTie,Ballgown处理转录组数据思路如下: 数据质控 将RNA-s ...

  8. 用PS绘制立体字的效果教程

    1. 新建一个透明画布. 2. 利用选区绘制一个圆圈 3. 利用渐变工具绘制渐变 (颜色自拟) 4. 选择混合器画笔工具按住ALT 点击填充颜色的图层 5. 新建画布填充黑色 利用混合画笔进行绘制. ...

  9. Spring ConversionService 类型转换(二) ConversionService

    Spring ConversionService 类型转换(二) ConversionService Spring 系列目录(https://www.cnblogs.com/binarylei/p/1 ...

  10. unity luaFramework

    1 AppConst: DebugMode: 调试模式,true:lua脚本直接读取自 AssetDir,false:开始会将AssetDir内的lua脚本复制到 Util.DataPath内(根据平 ...