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

迭代遍历:

head表示的是上一次处理完的节点,如果处理完的节点是栈头节点的子节点,就说明可以处理根节点了。(放的时候都是根右左,根在最下面,最后才会处理根)

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

Leetcode 145的更多相关文章

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

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

  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. 二叉树的后序遍历(Binary Tree Postorder Traversal)

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

  4. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

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

  5. LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

    翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...

  6. Java实现 LeetCode 145 二叉树的后序遍历

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

  7. Java for LeetCode 145 Binary Tree Postorder Traversal

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

  8. leetcode 145. Binary Tree Postorder Traversal ----- java

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

  9. Leetcode#145 Binary Tree Postorder Traversal

    原题地址 递归写法谁都会,看看非递归写法. 对于二叉树的前序和中序遍历的非递归写法都很简单,只需要一个最普通的栈即可实现,唯独后续遍历有点麻烦,如果不借助额外变量没法记住究竟遍历了几个儿子.所以,最直 ...

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

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

随机推荐

  1. Linux(CentOS)上面搭建Nginx环境

    总体上来说,Linux 这个系统其实挺好用的 除了看不见界面,但是用起来确实是比Window好用太多了,不废话了,直接说搭建环境的步骤! 安装Nginx 编译运行时的环境 参考博客:http://ww ...

  2. myCat知识笔记

    数据字典做成全局表(在各个分库中都有备份) ER表, 关联表都放在同一个分库上, 有利于数据关联查询 一致性hash ,/hash slot 主要为了解决分布式节点扩容时, 迁移数据的问题. mySq ...

  3. algorithm.sty not found error in LaTeX 解决方法

    参考: algorithm.sty not found error in LaTeX algorithm.sty not found error in LaTeX 解决方法 错误日志: LaTeX E ...

  4. uint8_t / uint16_t / uint32_t /uint64_t数据类型详解

    uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型? 在nesc的代码中,你会看到很多你不认识的数据类型,比如uint8_t等.咋一看,好像是个新的数据类型 ...

  5. Codeforces Round #219 (Div. 2) D. Counting Rectangles is Fun 四维前缀和

    D. Counting Rectangles is Fun time limit per test 4 seconds memory limit per test 256 megabytes inpu ...

  6. Redis的EXPIRE过期机制介绍

    概述在实际开发过程中经常会遇到一些有时效性数据,比如限时优惠活动,缓存或者验证码之类的.过了一段时间就需要删除这些数据.在关系型数据库中一般都要增加一个字段记录数据的到期时间,然后周期性地检查过期数据 ...

  7. 远程连接MySQL MySQL的远程连接

    在笔记本上安装了mysql, 想测试一下连接池对性能的影响,用了另一台PC来测试一段sql,出现以下错误: jdbc:mysql://10.201.11.128:3306/test Cannot cr ...

  8. linux系统磁盘使用情况

    #!/bin/bash total_disk=`df -k | grep -v Filesystem | awk '{print int($2)}'` available_disk=`df -k | ...

  9. 力扣(LeetCode) 14. 最长公共前缀

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...

  10. 【Linux】人脸识别的封装

    写了一个linux下的Face Recognition的封装,当作是练习.语言: C++的封装,结合opencv,使用方便.下载源码:https://github.com/zacario-li/Fac ...