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. 解决iframe加载的内容有时显示有时不显示

    在ASP.NET MVC项目中遇到了这样的一个问题,假设父页面有一个iframe <iframe id=" width="100%" height="10 ...

  2. jQuery统计上传文件的大小

    对于现代浏览器(支持html5)来说,在客户端统计上传文件的大小,可以通过$(selector)[0].files[0].size来实现.但在老版本的IE浏览器中,比如IE7,IE8或IE9,却不支持 ...

  3. Linux init 0-6 启动级别

    原文地址:http://blog.sina.com.cn/s/blog_5f8e8d9801010wlr.html 原文地址:[转]Linux init 0-6 启动级别作者:流水清风 init 0- ...

  4. 如何解决iOS6、iOS7 3.5寸和4.0寸屏的适配问题?不要写两个xib文件

    如何解决iOS6.iOS7 3.5寸和4.0寸屏的适配问题?不要写两个xib文件

  5. Map HashMap 排序 迭代循环 修改值

    HashMap dgzhMap = Dict.getDict("dgzh"); Iterator it_d = dgzhMap.entrySet().iterator(); whi ...

  6. 在Android工程中加入AIDL文件时,gen目录生成的文件报错-问题解决

    from://http://blog.csdn.net/watt520/article/details/10099047 今天在弄清除缓存的东东,按照网上别人的方法,创建了一个AIDL文件,这个时候发 ...

  7. LINQ to XML 建立,读取,增,删,改

      LINQ to XML的出现使得我们再也不需要使用XMLDocument这样复杂的一个个的没有层次感的添加和删除.LINQ可以使的生成的XML文档在内存中错落有致.下面以一个小的例子说名LINQ ...

  8. [Android Security] APK自我保护 - 代码乱序

    cp : https://segmentfault.com/a/1190000005095406 乱序原理 为了增加逆向分析的难度,可以将原有代码在 smali 格式上进行乱序处理同时又不会影响程序的 ...

  9. Unicode 和 UTF-8 的区别

    作者:于洋链接:https://www.zhihu.com/question/23374078/answer/69732605来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...

  10. Just-In-Time Debugging in Visual Studio 禁止VS在服务器上调试

    To disable Just-In-Time debugging by editing the registry On the Start menu, search for and run rege ...