这道题是LeetCode里的第145道题。

题目要求:

给定一个二叉树,返回它的 后序 遍历。

示例:

输入: [1,null,2,3]
1
\
2
/
3 输出: [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) {
stack<TreeNode*>st;
TreeNode *cur,*pre=NULL;
vector<int>res;
if(root==NULL)
return res;
cur=root;
st.push(cur);
while(st.size()!=0){
cur=st.top();
if((cur->left==NULL&&cur->right==NULL)||
(pre!=NULL&&(pre==cur->left||pre==cur->right))
){
res.push_back(cur->val);
st.pop();
pre=cur;
}
else{
if(cur->right!=NULL)
st.push(cur->right);
if(cur->left!=NULL)
st.push(cur->left);
}
}
return res;
}
};

提交结果:

个人总结:

后序遍历迭代法比较难,同样还是先左后右,和前序遍历中序遍历一起对照着学习进行总结会更好一点。

【LeetCode】Binary Tree Postorder Traversal(二叉树的后序遍历)的更多相关文章

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

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

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

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

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

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

  4. lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历

    题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...

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

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

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

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

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

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

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

    给定一棵二叉树,返回其节点值的后序遍历.例如:给定二叉树 [1,null,2,3],   1    \     2    /   3返回 [3,2,1].注意: 递归方法很简单,你可以使用迭代方法来解 ...

  9. Leetcode145. Binary Tree Postorder Traversal二叉树的后序遍历

    给定一个二叉树,返回它的 后序 遍历. 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class Solution { public: vector<int> res; ve ...

随机推荐

  1. 帝国empirecms数据库数据表详细说明

    表名   解释 phome_ecms_infoclass_news 新闻采集规则记录表 phome_ecms_infotmp_news 采集临时表 phome_ecms_news 新闻主数据记录表 p ...

  2. Centos安装iptables防火墙

    一.安装说明: 1.因为centos7.0及以上版本就默认安装了firewall防火墙,但有时候根据项目实际所需,服务器上还是需要安装iptables,以下就是具体的安装步骤: 2.因阿里云在服务器外 ...

  3. 如何删除 CentOS 6 更新后产生的多余的内核?

    第一种方法:通过命令的方式解决多余的内核 1.首先查看当前内核的版本号: [root@jxatei ~]# uname  -a Linux jxatei.server2.6.32-573.1.1.el ...

  4. 如何选择Web开发框架

    下面先来看看为什么要使用Web开发框架一 使用框架的必然性框架,即framework.其实就是某种应用的半成品,把不同应用程序中有共性的一些东西抽取出来,做成一个半成品程序,这样的半成品就是所谓的程序 ...

  5. 如何在SAP云平台的Cloud Foundry环境下添加新的Service(服务)

    我想在SAP云平台的Cloud Foundry环境下使用MongoDB的服务,但是我在Service Marketplace上找不到这个服务. cf marketplace返回的结果也没有. 解决方案 ...

  6. SAP Cloud for Customer Price-计价简介

    SAP Cloud for Customer(本文以下简称C4C)作为SAP新一代的CRM云产品,其Price功能实现虽不如以前的SAP ERP那么复杂,但是也能满足企业运作中各种Price需求. C ...

  7. [Docker] Docker安装和简单指令

    Docker笔记 安装 sudo apt install docker.io 启动和关闭Docker服务 # 启动Docker服务 sudo service docker start # 关闭Dock ...

  8. 剑指offer22 栈的压入、弹出序列

    写的一个代码,虽然正确通过了,但我觉得会报vector越界的错误 class Solution { public: bool IsPopOrder(vector<int> pushV,ve ...

  9. HTML_5 (1 2 3的代码总结)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. python之函数名的应用

    1. 函数名是一个特殊的变量 例题 例题1: a = 1 b = 2 c = a + b print(c) # 输出结果 3 # 总结 # 变量是否可以进行相加或者拼接操作是又后面指向的值来决定的,指 ...