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. 日志logging

    日志: 日志分为5个级别:debug(10),info(20),warning(30),error(40),critical(50) 日志四个组成部分:logger,handler,filter,fo ...

  2. LCD驱动分析(三)时序分析

    参考:S3C2440 LCD驱动(FrameBuffer)实例开发<一>   S3C2440 LCD驱动(FrameBuffer)实例开发<二>

  3. windows10系统下安装keras框架以theano为后端并配置gpu加速

    在安装之前,请确保你的显卡是NVIDIA的,并且是以下型号,否则不能进行gpu加速,右键我的电脑--管理--设备管理器--显示适配器.另外如果你的电脑是windows7,安装教程也是一样的,不过根据k ...

  4. 并查集--poj 2492

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

  5. C#保存图片到文件夹区分8位和24位

    1.保存图像--24位位图(显示的图像,包括增加结果到界面上的数据) Image image2 = default(Image); image2 = cogRecordDisplay1.CreateC ...

  6. BRVAH(让RecyclerView变得更高效) (3)

    本文来自网易云社区 作者:吴思博 3 实现列表加载动画效果    3.1默认动画 我们只需将自建的 adapter 继承它对应满足需求的 Adapter,然后在 Activity 中实例化,通过ope ...

  7. Oracle跟踪分析数据库启动的各个阶段

    目录 启动到nomount状态 设置trace 启动数据库到mount状态并打开 查阅trace 查阅trace的另外方法 v$diag_info 视图 演示如下: 启动到nomount状态 SYS@ ...

  8. [BZOJ2523][Ctsc2001]聪明的学生

    [BZOJ2523][Ctsc2001]聪明的学生 试题描述 一位教授逻辑学的教授有三名非常善于推理且精于心算的学生A,B和C.有一天,教授给他们三人出了一道题:教授在每个人脑门上贴了一张纸条并告诉他 ...

  9. [BZOJ1579] [Usaco2009 Feb]Revamping Trails 道路升级(分层图最短路 + 堆优化dijk)

    传送门 dis[i][j]表示第i个点,更新了j次的最短路 此题不良心,卡spfa #include <queue> #include <cstdio> #include &l ...

  10. K-lord #1

    Memories 布雷芙,和YYJ就那样在那个初夏相遇了. 她,一头蓬松的长发,简单可爱的短袖. 他,带着那么灿烂阳光的笑容. 于是一切就变得很微妙.眼神有了温度手心有了潮湿. 那些天空里匆忙盛开的夏 ...