原题地址

递归写法谁都会,看看非递归写法。

对于二叉树的前序和中序遍历的非递归写法都很简单,只需要一个最普通的栈即可实现,唯独后续遍历有点麻烦,如果不借助额外变量没法记住究竟遍历了几个儿子。所以,最直接的想法就是在栈中记录到底遍历了几个儿子。

代码:

 vector<int> postorderTraversal(TreeNode *root) {
vector<int> path;
stack<pair<TreeNode *, int> > st; st.push(pair<TreeNode *, int>(root, ));
while (!st.empty()) {
pair<TreeNode *, int> top = st.top();
st.pop(); if (!top.first)
continue; if (top.second == ) {
top.second = ;
st.push(top);
st.push(pair<TreeNode *, int>(top.first->left, ));
}
else if (top.second == ) {
top.second = ;
st.push(top);
st.push(pair<TreeNode *, int>(top.first->right, ));
}
else if (top.second == ) {
path.push_back(top.first->val);
}
} return path;
}

另外还有一种技巧性比较强的写法,只使用普通的栈就可以做到。利用到了一个后续遍历的特点,即:

后续遍历序列中,父节点前面一定紧挨着他的儿子节点(如果有的话)

所以,额外保存一个prev变量代表前一个遍历的节点,就可以识别出上面的情况。如果prev节点是当前节点的儿子节点(或者当前节点没有儿子),说明当前节点的儿子节点都遍历过了,可以遍历父节点了。

如何更新维护prev呢?当一个节点输出后,将prev置为这个节点,这是因为如果prev是后续遍历序列中的前一个节点,那么这个prev节点必须是已经被输出的节点。

代码如下:

     vector<int> postorderTraversal(TreeNode *root) {
vector<int> path;
stack<TreeNode *> st;
TreeNode *prev = NULL; st.push(root);
while (!st.empty()) {
TreeNode *node = st.top(); if (!node)
st.pop();
else if ((!node->left && !node->right)
|| (prev && (node->left == prev || node->right == prev))) {
path.push_back(node->val);
st.pop();
prev = node;
}
else {
st.push(node->right);
st.push(node->left);
}
} return path;
}

也就短了几行而已。。

Leetcode#145 Binary Tree Postorder Traversal的更多相关文章

  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. Example: Input: [1,null,2, ...

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

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

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

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

  5. Java for LeetCode 145 Binary Tree Postorder Traversal

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

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

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

  7. LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...

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

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

  9. 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

随机推荐

  1. 自定义获取html元素对象的7种方法。

  2. Java 为什么使用抽象类和接口

    Java接口和Java抽象类代表的就是抽象类型,就是我们需要提出的抽象层的具体表现.OOP面向对象的编程,如果要提高程序的复用率,增加程序的可维护性,可扩展性,就必须是面向接口的编程,面向抽象的编程, ...

  3. wing IDE破解方法

    WingIDE是我接触到最好的一款Python编译器了.但其属于商业软件,注册需要一笔不小的费用.因此,这篇简短的文章主要介绍了破解WingIDE的方法.破解软件仅供学习或者教学使用,如果您是商业使用 ...

  4. 学一点Git--20分钟git快速上手 [Neil]

    From: http://www.cnblogs.com/shuidao/p/3535299.html (图片已修复)在Git如日中天的今天,不懂git都不好意思跟人说自己是程序猿.你是不是早就跃跃欲 ...

  5. python的egg包的安装和制作]

    Defining Python Source Code Encodings Python egg 的安装 egg文件制作与安装 2011-06-10 14:22:50|  分类: python |   ...

  6. 编译mgiza的准备

    cmake之前需要首先设置环境变量: export BOOST_LIBRARYDIR=$BOOST_ROOT/lib64export BOOST_ROOT=/home/noah/boost_1_57_ ...

  7. 做HDU1010 带出来一个小问题

    做1010  本来是想的DFS深搜  但是自己凭空打  打不出来  因为没有记模板  然后就去搜  但是看了一遍  自己打却又是有BUG  然后验证  就出现了一个二维字符数组打印的问题 开始代码是这 ...

  8. 菜鸟学习Spring——60s让你学会动态代理原理

    一.为什么要使用动态代理         当一个对象或多个对象实现了N中方法的时候,由于业务需求需要把这个对象和多个对象的N个方法加入一个共同的方法,比如把所有对象的所有方法加入事务这个时候有三种方法 ...

  9. ASP.NET实现弹出框真分页将复选框选择的数据存到数据库中(四)

    这是第四步点击保存将信息存入数据库中. 这个就简单了利用ajax将JSON字符串传到后台然后这里有个知识点就是将DataTable直接存入数据库中.代码如下: 一.界面获取数据JS代码: //保存订单 ...

  10. Windows Phone 8.1SDK新特性预览

    前言    Windows Phone 8.1的预览版将在近期推送,WP 8.1的SDK也已经进入到RC阶段,可以从这里安装.本次更新的SDK被直接集成到了VS2013Update2里面,不再是单独的 ...