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. MySQL之mysql命令使用详解

    MySQL Name mysql - the MySQL command-line tool Synopsis mysql [options] db_name Description mysql is ...

  2. leetcode1:线性表

    //定义二维数组int **array = new int*[row_num]; ;i<row_num;i++) { array[i] = new int[col_num]; } vector& ...

  3. Group by、having、order by、Distinct 使用注意事项

    直奔主题,如下SQL语句 SELECT COUNT(*) AS COUNT,REQUEST,METHOD FROM REQUESTMETH GROUP BY REQUEST,METHOD HAVING ...

  4. PTA (Advanced Level) 1013 Battle Over Cities

    Battle Over Cities It is vitally important to have all the cities connected by highways in a war. If ...

  5. layer关闭弹出层返回值到父页面

    1.首先在父页面定义一个空间,Id=layerResult 然后 layer.open({ type: , title: '选择看课件', shadeClose: true, shade: 0.8, ...

  6. 文件流FileStram类

    本节课主要学习三个内容: 创建FileStram流 读取流 写入流 文件流FileStram类,是用来实现对文件的读取和写入.FileStram是操作字节的字节数组,当提供向文件读取和写入字节的方法时 ...

  7. Java基础教程(1)--概述

    一.什么是Java语言   Java是于1996年由Sun公司发布的一种极富创造力的面向对象的程序设计语言.它不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承.指针等概念,因此Java ...

  8. 五、standalone运行模式

    在上文中我们知道spark的集群主要有三种运行模式standalone.yarn.mesos,其中常被使用的是standalone和yarn,本文了解一下什么是standalone运行模式,它的运行流 ...

  9. Spring MVC 实现Excel的导入导出功能(1:Excel的导入)

    简介 这篇文章主要记录自己学习上传和导出Excel时的一些心得,企业办公系统的开发中,经常会收到这样的需求:批量录入数据.数据报表使用 Excel 打开,或者职能部门同事要打印 Excel 文件,而他 ...

  10. golang 的 sync.WaitGroup

    WaitGroup的用途:它能够一直等到所有的goroutine执行完成,并且阻塞主线程的执行,直到所有的goroutine执行完成. 官方对它的说明如下: A WaitGroup waits for ...