题目链接:https://leetcode-cn.com/problems/binary-tree-postorder-traversal/

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

示例:

输入: [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) {
vector<int> result;
if(root==nullptr) return result;
stack<TreeNode*> s;
TreeNode *p=root,*r=nullptr;
while(p||!s.empty()){
if(p){
s.push(p);
p=p->left;
}else{
p=s.top();
if(p->right&&p->right!=r){
p=p->right;
}else{
s.pop();
result.push_back(p->val);
r=p;
p=nullptr;
}
// if(p->right==nullptr||p->right==r){
// s.pop();
// result.push_back(p->val);
// r=p;
// p=nullptr;
// }else{
// p=p->right;
// }
}
}
return result;
}
};

LeetCode 145. 二叉树的后序遍历 (用栈实现后序遍历二叉树的非递归算法)的更多相关文章

  1. 二叉树(9)----打印二叉树中第K层的第M个节点,非递归算法

    1.二叉树定义: typedef struct BTreeNodeElement_t_ { void *data; } BTreeNodeElement_t; typedef struct BTree ...

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

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

  3. LeetCode 145 二叉树的后序遍历(非递归)

    题目: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路: 1 ...

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

    题目描述 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 后 ...

  5. PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由

    03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...

  6. 二叉树后序遍历的非递归算法(C语言)

    首先非常感谢‘hicjiajia’的博文:二叉树后序遍历(非递归) 这篇随笔开启我的博客进程,成为万千程序员中的一员,坚持走到更远! 折磨了我一下午的后序遍历中午得到解决,关键在于标记右子树是否被访问 ...

  7. [Leetcode] Construct binary tree from inorder and postorder travesal 利用中序和后续遍历构造二叉树

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:  You may assume th ...

  8. 【数据结构与算法】二叉树的 Morris 遍历(前序、中序、后序)

    前置说明 不了解二叉树非递归遍历的可以看我之前的文章[数据结构与算法]二叉树模板及例题 Morris 遍历 概述 Morris 遍历是一种遍历二叉树的方式,并且时间复杂度O(N),额外空间复杂度O(1 ...

  9. 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序

    接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...

随机推荐

  1. 在sublime text 3中搭建Java开发环境

    在jdk bin目录下新建一个bat文件: 如D:\JAVA\jdk1.8.0_65\bin\runJava.bat @ECHO OFF cd %~dp1 ECHO Compiling %~nx1.. ...

  2. mysql随机查询若干条数据

    条不重复的数据,使用以下: 秒以上 搜索Google,网上基本上都是查询max(id) * rand()来随机获取数据. SELECT *  FROM `table` AS t1 JOIN (SELE ...

  3. TypeError:Can't instantiate abstract class Ultraman with abstract methods sttack 报错

    报错Can't instantiate abstract class Ultraman with abstract methods sttack 通过非常仔细的排查,发现错误如下: 1.单词拼写错误是 ...

  4. java11类和对象

    import java.util.Scanner; public class jh_01_如何认识事物 { public static void main(String[] args) { Scann ...

  5. c++ 内存分配中一个有趣的小问题

    以下代码测试环境:vs2019 执行这么一段代码,看看会发生什么. int main() { ] = { }; arr[] = ; } 毫无疑问,会报错,因为访问越界了. 再看看另一段代码 ] = { ...

  6. const与vector的搭配

    有三种组合方式,分别为: vector<const int> vec; const vector<int> vec; const vector<const int> ...

  7. Uncaught Error: Call to undefined function mcrypt_get_iv_size() 解决办法

    函数 mcrypt_get_iv_size 在只在(PHP 4 >= 4.0.2, PHP 5, PHP 7 < 7.2.0, PECL mcrypt >= 1.0.0) 这几个版本 ...

  8. [转载]理解weight decay

    http://blog.sina.com.cn/s/blog_a89e19440102x1el.html

  9. 《python可以这样学》第一章

    一.Python基础 查看Python版本 Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AM ...

  10. C#基础知识学习(2)string类中的方法

    1.Compare 比较字符串 用来比较2个字符串的长度大小和值是否相同,相同则返回0,当x比y小返回-1,否则返回1,如果长度相同,且值不同,则返回1,代码如下 public static void ...