C++,递归

 /**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
/**
* @param root: The root of binary tree.
* @return: Postorder in vector which contains node values.
*/
public:
vector<int> postorderTraversal(TreeNode *root) {
// write your code here
vector<int> result;
if (root == NULL) {
return result;
}
if (root->left != NULL) {
vector<int> left = postorderTraversal(root->left);
result.reserve(result.size() + left.size());
result.insert(result.end(), left.begin(), left.end());
}
if (root->right != NULL) {
vector<int> right = postorderTraversal(root->right);
result.reserve(result.size() + right.size());
result.insert(result.end(), right.begin(), right.end());
}
result.push_back(root->val);
return result;
}
};

C++,递归,辅助函数

 /**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
/**
* @param root: The root of binary tree.
* @return: Postorder in vector which contains node values.
*/
public:
vector<int> postorderTraversal(TreeNode *root) {
// write your code here
vector<int> result;
if (root == NULL) {
return result;
} else {
postorderCore(root, result);
}
return result;
}
void postorderCore(TreeNode *root, vector<int> &result) {
if (root == NULL) {
return;
}
if (root->left != NULL) {
postorderCore(root->left, result);
}
if (root->right != NULL) {
postorderCore(root->right, result);
}
result.push_back(root->val);
return;
}
};

C++,非递归

[一个stack]

[一个cur指针]

[一个pre指针]

 /**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
/**
* @param root: The root of binary tree.
* @return: Postorder in vector which contains node values.
*/
public:
vector<int> postorderTraversal(TreeNode *root) {
// write your code here
vector<int> result;
if (root == NULL) {
return result;
} TreeNode *cur = root, *pre = NULL;
stack<TreeNode *> sta; while (cur != NULL || !sta.empty()) {
while (cur != NULL) {
sta.push(cur);
cur = cur->left;
}
cur = sta.top();
if (cur->right == NULL || cur->right == pre) {
sta.pop();
result.push_back(cur->val);
pre = cur;
cur = NULL;
} else {
cur = cur->right;
}
}
return result;
}
};

LintCode: Binary Tree Postorder Traversal的更多相关文章

  1. 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal

    详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal            Given a binary tree, return the po ...

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

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

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

  4. LeetCode: Binary Tree Postorder Traversal 解题报告

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  5. 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  6. 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  7. LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

    145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...

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

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

  9. 145. Binary Tree Postorder Traversal

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

随机推荐

  1. Task Parallel Library02,更进一步

    在前一篇中,了解了Task的基本用法 如果一个方法返回Task,Task<T>,如何获取Task的返回值,获取值的过程会阻塞线程吗? static void Main(string[] a ...

  2. 技术人生:Knowing when or where it’s appropriate to use a technique or tool is just as important as knowing how to use it.

    Knowing when or where it’s appropriate to use a technique or tool is just as important as knowing ho ...

  3. 老美的zxing和日本的qrcode哪个好?

    ZXing用Java实现的多种格式的1D/2D条码图像处理库,Zxing库的主要部分支持以下几个功能:核心代码的使用.适用于J2SE客户端的版本.适用于Android客户端的版本(即BarcodeSc ...

  4. centOS配置国内镜像

    本文以163为例,  cd /etc/yum.repos.d/wget http://mirrors.163.com/.help/CentOS6-Base-163.repo 

  5. 多个so中模板单例的多次实例化

    在Android打包项目时,发现登录功能不能使用了,logcat中也没发现什么问题,最后一行一行log定位到了问题.原来是一个so文件中的构造函数被初始化二次!   这个单例是通过继承模板来实现的(暂 ...

  6. hdu 2049 不easy系列之(4)——考新郎

    不easy系列之(4)--考新郎 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. ntp测试

    cmd下 w32tm /stripchart /computer:time1.aliyun.com linux ntpdate ntp1.aliyun.com

  8. 通过AnimationSet设置动画

    在代码中可以通过set来设置多个动画属性,这里分开来设置不同的属性. 首先先贴上布局文件,里面的imageview是用来做动画的控件 <RelativeLayout xmlns:android= ...

  9. centos7更改为启动桌面或命令行模式

    进入cenos7的命令行模式 终端输入“init 3”回车进入命令行模式 登录成功后 # systemctl get-default //获取当前系统启动模式 查看配置文件 # cat /etc/in ...

  10. MySQL中的information_schema数据库表说明

    MySQL 中的 information_schema 数据库   版权声明:https://blog.csdn.net/kikajack/article/details/80065753 1. 概述 ...