题目:

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

Note: Recursive solution is trivial, could you do it iteratively?

说明:

1) 两种实现,递归与非递归 , 其中非递归有两种方法

2)复杂度分析:时间O(n)、空间O(n)

实现:

一、递归

 /**
* Definition for binary tree
* 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> root_vec;
vector<int> left_vec;
vector<int> right_vec;
if(root==NULL) return root_vec;
if(root->left) left_vec=postorderTraversal(root->left);
if(root->right) right_vec=postorderTraversal(root->right);
root_vec.push_back(root->val);
left_vec.insert(left_vec.end(),right_vec.begin(),right_vec.end());
left_vec.insert(left_vec.end(),root_vec.begin(),root_vec.end());
return left_vec;
}
};

二、非递归

根据后序遍历的顺序,先访问左子树,再访问右子树,后访问根节点,而对于每个子树来说,又按照同样的访问顺序进行遍历,后序遍历的非递归的实现相对来说要难一些,要保证根节点在左子树和右子树被访问后才能访问,思路如下:

对于任一节点P,

1)先将节点P入栈;

2)若P不存在左孩子和右孩子,或者P存在左孩子或右孩子,但左右孩子已经被输出,则可以直接输出节点P,并将其出栈,将出栈节点P标记为上一个输出的节点,再将此时的栈顶结点设为当前节点;

3)若不满足2)中的条件,则将P的右孩子和左孩子依次入栈,当前节点重新置为栈顶结点,之后重复操作2);

4)直到栈空,遍历结束。

a、下面代码比较常规,与上面分析思路一致

 /**
* Definition for binary tree
* 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> postorder_vec;
TreeNode *cur=root; //定义指针,指向当前节点
TreeNode *pre=NULL;//定义指针,指向上一各访问的节点
if(cur==NULL) return postorder_vec;
stack<TreeNode *> postorder_stack;//创建一个空栈
postorder_stack.push(cur);//先将树的根节点入栈
//直到栈空时,结束循环
while(!postorder_stack.empty())
{
cur=postorder_stack.top();//当前节点置为栈顶节点
if((cur->left==NULL&&cur->right==NULL)||
((pre!=NULL)&&(cur->left==pre||cur->right==pre)))
{
//如果当前节点没有左右孩子,或者有左孩子或有孩子,但已经被
//访问输出,则直接输出该节点,将其出栈,将其设为上一个访问的节点
postorder_stack.pop();
postorder_vec.push_back(cur->val);
pre=cur;
}
else
{
//如果不满足上面两种情况,则将其右孩子左孩子依次入栈
if(cur->right!=NULL) postorder_stack.push(cur->right);
if(cur->left!=NULL) postorder_stack.push(cur->left);
}
}
}
};

b、下面代码简洁(个人感觉,不喜勿喷)

 /**
* Definition for binary tree
* 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> rs;
if (!root) return rs; //若为空树,则返回空vector
stack<TreeNode *> stk;
stk.push(root); //当前节点入栈
while (!stk.empty())
{
TreeNode *t = stk.top(); //栈顶节点出栈、输出
stk.pop();
rs.push_back(t->val);
//注意,下面入栈顺序不能错 ,因为先左后右,
//这样出栈时先遍历才是右(中->右->左)
if (t->left) stk.push(t->left);
if (t->right) stk.push(t->right);
}
reverse(rs.begin(), rs.end()); //逆序,就成了后序遍历了
return rs;
}
};

leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)的更多相关文章

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

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

  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二叉树的后序遍历 (C++)

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

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

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

  5. 【LeetCode】Binary Tree Postorder Traversal(二叉树的后序遍历)

    这道题是LeetCode里的第145道题. 题目要求: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...

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

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

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

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

  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. 利用UIImagePickerController或者利用UIKit的 UIGraphicsBeginImageContext保存图片

    转载自:http://my.oschina.net/hmj/blog/99970    应用中有时我们会有保存图片的需求,如利用UIImagePickerController用IOS设备内置的相机拍照 ...

  2. android 开源 + 一些素材网站

    ui 设计工具:http://www.sketchcn.com/ 分类汇总: https://github.com/Trinea/android-open-project 直接拿来用!最火的Andro ...

  3. [ASP.NET]更简单的方法:FormsAuthentication登录ReturnUrl使用绝对路径

    转自:http://www.cnblogs.com/dudu/p/formsauthentication-returnurl-absoluteuri.html [ASP.NET]更简单的方法:Form ...

  4. C# DataTable.Select() 筛选数据

    有时候我们需要对数据表进行筛选,微软为我们封装了一个公共方法, DataTable.Select(),其用法如下: Select() Select(string filterExpression) S ...

  5. Oracle和plsql developer编码设置

    在使用pl/sql developer时,查询出来中文字段显示乱码,因为数据库的编号格式和pl /sql developer的编码格式不统一造成的. 一.查看和修改oracle数据库字符集 selec ...

  6. Windows 错误代码

    Error Messages for Windows http://www.gregorybraun.com/MSWINERR.ZIP Server 4.0 Error Messages   Code ...

  7. X431 元征诊断枪

    X-431 Diagun是专门为汽车维修技师设计的诊断设备. 小巧的主机.强大的诊断功能.方便快捷的网上升级.一体化多功能接头,都是维修技师的首选.X-431 Diagun 是汽车维修技师的标准装备. ...

  8. SCCM2007

    Active Directory系统组发现:此方法按照上次运行发现方法时 Active Directory 中的响应返回对象,可发现活动目录OU.全局组.通用组.嵌套组.非安全组. Active Di ...

  9. UVALive 4222 Dance 模拟题

    Dance 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&pag ...

  10. HDU 5578 Friendship of Frog 水题

    Friendship of Frog Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.ph ...