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. INDY10的IDHttpServer应答客户端

    INDY10的IDHttpServer应答客户端 首先贴源码: procedure TIdHTTPResponseInfo.WriteContent; begin if not HeaderHasBe ...

  2. cocos2d-x 3.0 WIN7+VS2012 安卓平台搭建

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  3. 【 D3.js 入门系列 --- 2 】 怎样使用数据和选择元素

    本人的个人博客首页为: http://www.ourd3js.com/  ,csdn博客首页为:http://blog.csdn.net/lzhlzz/. 转载请注明出处,谢谢. 接着上一讲的内容,这 ...

  4. 限制EditText必须输入中文的方法

    给EditText做限制时,我们想要输入的字符串必须都是中文,不出现任何其他字符,下面的类可以很好的实现这个要求. InputLenLimit.java package com.kale.button ...

  5. Intelli公司IDE快捷键设置

    自定义快捷键:Keymap 搜索某个像自定义的快捷键,看被占用了没有:搜索时,直接键盘敲击快捷键,比如command+T

  6. Chapter 4 -- Throwables

    TODO: rewrite with more examples Guava's Throwables utility can frequently simplify dealing with exc ...

  7. Java NIO SocketChannel

    A Java NIO SocketChannel is a channel that is connected to a TCP network socket. It is Java NIO's eq ...

  8. 浅谈Hybrid技术的设计与实现【转】

    https://www.cnblogs.com/yexiaochai/p/4921635.html 前言 浅谈Hybrid技术的设计与实现 浅谈Hybrid技术的设计与实现第二弹 浅谈Hybrid技术 ...

  9. CSS-返回顶部代码

    现在的网站基本上都是长页面,多的有四五屏,少的话也有两三屏,页面太长有的时候为了提升用户体验,会在页面右边出现一个回到顶部的按钮,这样能快速回到顶部,以免在滑动页面出现视觉屏幕,回到顶部一般有四种方式 ...

  10. em px pt单位介绍及换算

    PX\EM\PT单位介绍 px Pixel单位名称为像素,相对长度单位,像素(px)是相对于显示器屏幕分辨率而言的国内推荐:em单位名称为相对长度单位.相对于当前对象内文本的字体尺寸,国外使用比较多, ...