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].

解题:

二叉树的后序遍历,比先序和中序遍历稍显麻烦一些,有兴趣可查看:二叉树先序遍历二叉树中序遍历

后序遍历需要输出左子树后,输出右子树,最后输出当前结点。复杂的原因是,输出左右子树后,如何判断输出的结点是左还是右。如果左结点已经输出,就继续输出右;如果右结点已经输出,就输出当前结点,并pop出栈中上一层的结点继续考察。因此,后序遍历和先中序遍历代码上主要不同是:

1、引入lastNode指针,指向刚刚输出的结点,方便判断刚刚输出的结点是上一层的左儿子还是右儿子;

2、如果一个结点,连同其左子树和右子树都已经输出完毕,应继续pop栈中元素,但是新pop出的结点,在下一次循环中不应再循环遍历其左子树。

因此在一个结点连同其子树都输出完毕后,将curNode置为0,这样跳过循环头部的“循环查找左子树”过程之后再pop栈中元素,从而直接进入判定阶段。

代码:

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

【Leetcode】【hard】Binary Tree Postorder Traversal的更多相关文章

  1. 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

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

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

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

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

  4. LeetCode: Binary Tree Postorder Traversal 解题报告

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  5. 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal

    详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal            Given a binary tree, return the po ...

  6. Binary Tree Preorder Traversal and Binary Tree Postorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  7. 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator

    144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  8. [Leetcode 144]二叉树前序遍历Binary Tree Preorder Traversal

    [题目] Given a binary tree, return the preordertraversal of its nodes' values. Example: Input: [1,null ...

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

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

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

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

随机推荐

  1. 深度学习(八) Batch Normalization

    一:BN的解释:  定义: 顾名思义,batch normalization嘛,就是“批规范化”咯.Google在ICML文中描述的非常清晰,即在每次SGD时,通过mini-batch来对相应的act ...

  2. json和xml以及ajax的数据格式用法

    JSON的两个方法: 1.将字符串转换为JSON格式:parse(). 2.将原生JavaScript值转换为JSON字符串:stringify(); <!DOCTYPE html> &l ...

  3. SQL Serever学习10——T-SQL语句

    在sqlserver2018中使用的是Transact-SQL语言,简称T-SQL. 数据库的创建和管理 数据定义语言DDL DDL功能包括数据库,表,索引,视图,存储过程 数据库:CREATE DA ...

  4. 【13】MD5编码、Zlib压缩解压缩

    1.MD5加密 /// <summary> /// 使用MD5加密算法 /// </summary> /// <param name="md5MessageSt ...

  5. 微信小程序(一)--简单的介绍

    转自:https://developers.weixin.qq.com/miniprogram/dev/index.html 响应的数据绑定 框架的核心是一个响应的数据绑定系统. 整个小程序框架系统分 ...

  6. 引入第三方js文件,报错

    错误:Mixed Content: The page at 'https://localhost:44336/MENU' was loaded over HTTPS, but requested an ...

  7. ASP.Net 之委托事件

    1.首先给一张图让大家了解什么是委托?它的优缺点是什么? 2.通过代码的运用更深入地了解委托事件(窗体应用程序) 1)下面我们先定义一个无参数的委托. //1.0 定义一个自定义的委托,此委托的签名是 ...

  8. Git建立独立分支

    前言 在码云建立git项目后默认分支是master, 这里如果直接在码云新建分支, 会指定默认分支; 所以通过git 命令git checkout --orphan 新分支名 创建独立分支 创建 创建 ...

  9. 深入理解LinkedBlockingQueue

      说明 通过阅读源码,了解LinkedBlockingQueue的特性.本文基于JDK1.7源码 正文 通过查询API对LinkedBlockingQueue特点进行简单的了解: LinkedBlo ...

  10. oracle 多列数据相同,部分列数据不同合并不相同列数据

    出现这样一种情况: 前面列数据一致,最后remark数据不同,将remark合并成 解决办法: 最后一列:结果详情: 使用到的语句为: select a,b,c,wm_concat(d) d,wm_c ...