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

示例:

输入: [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) {}
* };
*/
/*
算法思想:
采用递归的思想,即借助系统栈,效率较低。二叉树的前序遍历规则: 1. 遍历左子树;2. 遍历右子树;3. 访问根结点;
*/
/*
class Solution {
private:
void rec(TreeNode* root,vector<int> &ret){
if(root != NULL){
rec(root->left,ret);
rec(root->right,ret);
ret.push_back(root->val);
}
}
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> ret;
rec(root,ret);
return ret;
}
};
*/
/*
算法思想:
采用迭代的方法,使用了一个辅助结点p,这种写法其实可以看作是一个模版,对应的还有前序和中序的模版写法,形式很统一,方便于记忆。
由于后序遍历的顺序是左-右-根,而前序遍历的顺序是根-左-右,二者其实还是很相近的,我们可以先在先序遍历的方法上做些小改动,使其遍历顺序变为根-右-左,然后翻转一下,就是左-右-根了。翻转的方法,是反向加入结果res,每次都在结果res的开头加入结点值,而改变先序遍历的顺序就只要改变一下入栈顺序,先左后右,这样出栈处理的时候就是先右后左了。一定要对比前序遍历记忆!!! 拓展:当访问一个结点*p时,栈中结点恰好为*p结点的所有祖先。从栈底到栈底结点再加上*p结点,刚好构成从根节点到*p结点的一条路径。这一特性非常重要,如求根结点到某结点的路径;求两个结点的最近公共祖先;均可用这个思想。
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
stack<TreeNode*> s;
TreeNode *p = root;
while (!s.empty() || p) {
if (p) {
s.push(p);
res.insert(res.begin(), p->val); //反向添加,而前序是正向添加
p = p->right;
} else {
TreeNode *t = s.top();
s.pop();
p = t->left;
}
}
return res;
}
};

LeetCode145 二叉树的后序遍历的更多相关文章

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

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

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

    145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...

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

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

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

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

  5. LintCode-68.二叉树的后序遍历

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

  6. PTA L2-006 树的遍历-二叉树的后序遍历+中序遍历,输出层序遍历 团体程序设计天梯赛-练习集

    L2-006 树的遍历(25 分)   给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤),是二叉树中结点的 ...

  7. LeetCode:二叉树的后序遍历【145】

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

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

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

  9. leecode刷题(30)-- 二叉树的后序遍历

    leecode刷题(30)-- 二叉树的后序遍历 二叉树的后序遍历 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 思路 ...

随机推荐

  1. 合并EXCEL文件到一个文件的V宏

    我建的宏: Sub 合并文件的VBA() Dim MyPath, MyName, AWbName Dim Wb As Workbook, WbN As String Dim G As Long Dim ...

  2. qq 表情库

    ![/qq](https://cdn.luogu.com.cn/upload/pic/62224.png) ![/cy](https://cdn.luogu.com.cn/upload/pic/622 ...

  3. Codeforces Edu Round 64 A-D

    A. Inscribed Figures 分类讨论打表即可. PS:这道题翻译有歧义. 这样稍微翻转一下,就可以是\(7\)个交点呀...(大概是我没看英文题干导致的惨案) #include < ...

  4. STL——容器(Map & multimap)的大小

    1. Map & multimap 的大小 map.size();     //返回容器中元素的数目 map.empty();//判断容器是否为空, 容器中有内容将会返回 false 代码示例 ...

  5. celery定时执行任务 的使用

    1 参照博客 https://www.cnblogs.com/xiaonq/p/9303941.html#i1 1 创建celery_pro包   # 可在任意文件下 2 在 celery_pro 下 ...

  6. 2020-2021-1 20209307 《Linux内核原理与分析》第二周作业

    1.寻址方式和常用汇编指令 寄存器寻址:movl %eax,%edx  相当于edx=eax 立即寻址:movl $0x123,%edx   相当于edx=0x123 直接寻址:movl 0x123, ...

  7. Python SQLALchemy框架

    SQLALchemy SQLALchemy是Python中的一款优秀的ORM框架,它可以作用于任何第三方Web框架,如flask,tornado等框架. SQLALchemy相较于DjangoORM来 ...

  8. Java进阶专题(十九) 消息中间件架构体系(1)-- ActiveMQ研究

    前言 MQ全称为Message Queue,即消息队列,它是一种应用程序之间的通信方法,消息队列在分布式系统开 发中应用非常广泛.开发中消息队列通常有如下应用场景:1.任务异步处理.将不需要同步处理的 ...

  9. angular8

    @Component 装饰器告诉Angular , AppComponent 类是一个组件,装饰器的属性用于配置该组件的应用方式. selectot 属性告诉Angular如何在HTML文档中应用该组 ...

  10. "Date has wrong format. Use one of these formats instead: %, Y, -, %, m, -, %, d." DateField使用input_formats参数

    错误写法 : publish_date = serializers.DateField(format="%Y-%m-%d", input_formats="%Y-%m-% ...