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?

递归方法

 /**
* 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) {
if(root==NULL)
return v;
TreeNode *left=root->left,*right=root->right;
postorderTraversal(left);
postorderTraversal(right);
v.push_back(root->val);
return v;
} vector<int> v;
};

非递归

处理并出栈时判断,该节点是否有子节点或其子节点是否被访问过。并记录上次处理的节点。

 /**
* 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> v;
if(root==NULL)
return v;
stack<TreeNode *> tree;
tree.push(root);
TreeNode *pre=NULL;
while(!tree.empty()){
TreeNode *p = tree.top();
if((p->left==NULL&&p->right==NULL)||(pre!=NULL&&(p->left==pre||p->right==pre))){
v.push_back(p->val);
pre=p;
tree.pop();
}else{
if(p->right!=NULL)
tree.push(p->right);
if(p->left!=NULL)
tree.push(p->left);
}
}
return v;
}
};

binary-tree-postorder-traversal——二叉树后续遍历的更多相关文章

  1. leetcode Binary Tree Postorder Traversal 二叉树后续遍历

    先给出递归版本的实现方法,有时间再弄个循环版的.代码如下: /** * Definition for binary tree * struct TreeNode { * int val; * Tree ...

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

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

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

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

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

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

  5. [Leetcode] Binary tree postorder traversal二叉树后序遍历

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Python模块概念

    补充:生成器表达式 将列表生成器的中括号改为小括号就是生成器表达式 res = [i for i in range(10) if i > 5]  #  列表生成式 res = (i for i ...

  2. C#显示相机实时画面

    public partial class Form1 : Form { ICogAcqFifo mAcqFifo2;//定义一个相机对象 private ICogFrameGrabber mFrame ...

  3. Exchange 2010 打补丁的顺序

    升级的顺序: 客户端访问服务器 ——> 集线器传输服务器 ——>统一消息服务器(如果有的话)——> 邮箱服务器 ——> 边缘服务器.

  4. 大数据学习——scala入门程序

    安装scala.msi https://blog.csdn.net/sinat_32867867/article/details/80305302 notepad++ object HelloScal ...

  5. mysqli 封装

    <?php //header header('content-type:text/html;charset=UTF-8'); class DB { //定义属性 private $host;// ...

  6. vue 组件高级用法实例详解

    一.递归组件 组件在它的模板内可以递归地调用自己, 只要给组件设置name 的选项就可以了. 示例如下: <div id="app19"> <my-compone ...

  7. 【边双连通】poj 3352 Road Construction

    http://poj.org/problem?id=3352 [题意] 给定一个连通的无向图,求最少加多少条边使得这个图变成边双连通图 [AC] //#include<bits/stdc++.h ...

  8. Ubuntu 常用命令和一些 tips

    001. ubuntu 解压.tar.xz文件到另一个文件夹:sudo tar -xvJf ***.tar.xz -C /usr/src sudo 超级用户tar [选项...][file]...-x ...

  9. Spring-IOC源码解读2-容器的初始化过程

    1. IOC容器的初始化过程:IOC容器的初始化由refresh()方法启动,这个启动包括:BeanDifinition的Resource定位,加载和注册三个过程.初始化的过程不包含Bean依赖注入的 ...

  10. android开发里跳过的坑——TimePickerDialog onTimeSet不回调

    在android6.0.1上测试发现TimePickerDialog的onTimeSet和DatePickerDialog的onDateSet不回调,查看SDK源码发现,TimePickerDialo ...