【Leetcode】【hard】Binary Tree Postorder Traversal
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的更多相关文章
- 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
- LeetCode: Binary Tree Postorder Traversal 解题报告
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
- 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 ...
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- [Leetcode 144]二叉树前序遍历Binary Tree Preorder Traversal
[题目] Given a binary tree, return the preordertraversal of its nodes' values. Example: Input: [1,null ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
随机推荐
- 第1章—Spring之旅—简化Spring的java开发
简化Spring的java开发 1.1简介 区别于EJB的特性 简化javaBean,为了降低java开发的复杂性,Spring采取了以下4种关键策略: 基于POJO的轻量级和最小入侵性编程 通过依赖 ...
- mongo 授权访问
1.授权远程也可以访问 - 首先修改mongodb的配置文件 让其监听所有外网ip 编辑文件:/etc/mongodb.conf 修改后的内容如下: bind_ip = 0.0.0.0 port = ...
- 利用setTimeout来实现setInterval
在Js中,当我们要在一定间隔时间内不断执行同一函数,我们可以使用setInterval函数,但setInterval在某些情况下使用时也存在一定问题. 1.不去关心回调函数是否还在运行 在某些情况下, ...
- Java reflect 反射 0 java对象的三个阶段
- redis的数据类型(二)string类型
下面讲解value,value包括String.List.Set.Sorted Set.Hash 一.String类型 1.string类型 String是最基本的类型,而且Stirng类型是二 ...
- MyBatis Generator 详解(转)
MyBatis Generator中文文档 MyBatis Generator中文文档地址:http://mbg.cndocs.tk/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看中 ...
- 转:Java 基本数据类型
Java 基本数据类型 转:http://www.runoob.com/java/java-basic-datatypes.html 变量就是申请内存来存储值.也就是说,当创建变量的时候,需要在内存中 ...
- 单独烧录kernel
使用fastboot: adb reboot bootloader //adb 进入BootLoader(fastboot是BootLoader的命令) fastboot flash boot boo ...
- SpringMVC内置的精准数据绑定2
https://blog.csdn.net/flashflight/article/details/42935137 之前写过一篇<扩展SpringMVC以支持更精准的数据绑定1>用于完成 ...
- java中equal和==的比较
equals 方法是 java.lang.Object 类的方法. 有两种用法说明: (1)对于字符串变量来说,使用“==”和“equals()”方法比较字符串时,其比较方法不同. “==”比较两个变 ...