原题地址

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

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

代码:

 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. java高级工程师必备知识

    成为Java高级工程师需要掌握哪些核心点? 每 逢长假都会有很多程序员跳槽,十一.过年是跳槽黄金时刻,尤其是过年.过年的时候年终奖到手,没有了多少牵挂,年终同学同事聚会比较多,沟通的就多,各种 工作机 ...

  2. MapReduce作业的map task和reduce task调度参数

    MapReduce作业可以细分为map task和reduce task,而MRAppMaster又将map task和reduce task分为四种状态: 1.pending:刚启动但尚未向reso ...

  3. Decorator模式

    Decorator模式能够像标准的继承一样为类添加新的功能. 不同于标准继承机制的是,如果对象进行了实例化,Decorator模式能够在运行时动态地为对象添加新的功能. <?php abstra ...

  4. 魅族MX3问题集锦

    我第一台智能机已经服役2年半了,已经满足不了现在日益庞大的APP,所以打算让他光荣退役.我觉得IPhone仍然是目前做的最好的手机,但是对于我来说好像没什么必要,尤其那土豪般的价格.而且我平时看视频居 ...

  5. Windows7不能打开telnet功能

    在dos窗口中输入telnet命令提示如下: 解决方法是打开控制面板中的程序和功能,选择打开或关闭Window是功能,如下: 勾选上面的Telnet客户端,然后确定,出现下图.稍等片刻重新打开命令行, ...

  6. 使用WIF实现单点登录Part III —— 正式实战

    我们接下来的demo将包括以下的工程: SiteA —— 基于.net framework 4.5的MVC 4程序,使用WIF 4.5的SDK,第一个RP SiteB —— 基于.net framew ...

  7. N进制数组转换成正整数

    给定一个任意长度的数组,其中的元素按照一定的进制(N进制)来转换成正整数 //把数组中的元素按照N进制转换成为正整数 #include <stdio.h> #include <std ...

  8. jquery.unobtrusive-ajax.js单独的用法

    (插件本身已经减少了人力,如果开始无脑开发,简直就是无能,@Ajax里面哪里帮助类生成的其实就是jquery.unobtrusive的一些特性) jquery.unobtrusive是MVC中出现的j ...

  9. 几道华为经典C语言面试题

    1.找错 void test1() { char string[10]; char* str1="0123456789"; strcpy(string, str1); } 这里st ...

  10. MIFARE系列7《安全性》

    飞利浦的MIFARE卡由于它的高安全性在市场上得到广泛应用,比如我们乘车用的公交卡,学校和企业食堂的饭卡等等.它每个扇区有独立的密匙(6个字节的密码),在通信过程中首先要验证密匙才能读写数据.它的关键 ...