1.后序遍历的非递归实现。(左右根)

难点:后序遍历的非递归实现是三种遍历方式中最难的一种。因为在后序遍历中,要保证左孩子和右孩子都已被访问并且左孩子在右孩子前访问才能访问根结点,这就为流程的控制带来了难题。下面介绍两种思路。

思路:有个关键的就是unUsed这个标识符。

当unUsed=1时,表示该节点未遍历过,即以该节点为根节点的左右孩子不曾遍历。

当unUsed=0时,表示该节点的左右孩子都已经被访问过。

由于入栈的顺序和出栈的顺序相反,所以若unUsed=1,则左根右节点依次入栈,且根节点的unUsed置为0.

代码:

class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> res;
if(root==NULL) return res;
stack<pair<TreeNode*,int>> s;
int unUsed;
s.push(make_pair(root,)); while (!s.empty())
{
root=s.top().first;
unUsed=s.top().second;
s.pop();
if(unUsed){
s.push(make_pair(root,));
if(root->right)
s.push(make_pair(root->right,));
if(root->left)
s.push(make_pair(root->left,));
}else{
//cout<<root->val<<" ";
res.push_back(root->val);
}
}
}
};

2.拓展,中序遍历的非递归实现(左根右)。和以上思路类似,由于也不能先访问根节点,但是我们是从根节点出发,所以还是使用unUsed来标志根节点的状态。

往栈中放节点的顺序倒过来即:左根右,且该根节点的左右孩子访问完毕。

代码:

void inOrder(Node *p)
{
if(!p)
return;
stack< pair<Node*,int> > s;
Node *t;
int unUsed;
s.push(make_pair(p,));
while(!s.empty())
{
t=s.top().first;
unUsed = s.top().second;
s.pop();
if(unUsed)
{
if(t->right)
s.push( make_pair(t->right,) );
s.push( make_pair(t,) );
if(t->left)
s.push( make_pair(t->left,));
}
else printf("%d\n",t->data);
}
}

3.前序非递归,直接先根节点。

void preOrder(Node *p) //非递归
{
if(!p) return;
stack<Node*> s;
Node *t;
s.push(p);
while(!s.empty())
{
t=s.top();
printf("%d\n",t->data);
s.pop();
if(t->right) s.push(t->right);
if(t->left) s.push(t->left);
}
}

Binary Tree Postorder Traversal(各种非递归实现,完美利用栈结构模拟)的更多相关文章

  1. Binary Tree Preorder Traversal (非递归实现)

    具体思路参见:二叉树的非递归遍历(转) 先序遍历(根左右). 即把每一个节点当做根节点来对待. /** * Definition for binary tree * struct TreeNode { ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 坑爹的鲁大师,VMware Workstation 报错(AsyncSocket error)一例解决

    今天准备把电脑上安装的VMware Play换成VMware Workstation,毕竟 Workstation 的快照功能还是很有必要的. 结果,VMware Workstation 安装成功后, ...

  2. dede自定义表单放首页出错的解决办法

    一.当自定义表单放首页提交的时候跳出这个页面怎么解决 二.解决办法 可能有多个from表单提交出错,也就是代码冲突的意思,只要把代码检查好,from提交不要重复冲突就可以了

  3. iOS Programming View Controllers 视图控制器

    iOS Programming View Controllers  视图控制器  1.1  A view controller is an instance of a subclass of UIVi ...

  4. MySQL学习随笔--视图

    视图概念 数据库中的视图指的是一个虚拟表,其内容由查询定义.同真实的表一样,视图也是由行与列构成的.视图的数据来源由SQL语句查询得到,不存储数据 视图创建方法 格式 : create view 视图 ...

  5. SQL Server 零散笔记

    排序显示行号 select Row_Number() over(order by Code) as RowNumber,ID,Code,Name from CBO_ItemMaster 不排序显示行号 ...

  6. Node.js——路径问题

    相对路径问题:读取文件,形如 ./ 或者 ../  一般认为这个点是相对于这个 js 文件的文件夹路径,实际上不是,这个点相对于node命令时的当前路径,不同 js 文件下进行 ./  这样的引入是不 ...

  7. Python_高阶函数、装饰器(decorator)

    一.变量: Python支持多种数据类型,在计算机内部,可以把任何数据都看成一个“对象”,而变量就是在程序中用来指向这些数据对象的,对变量赋值就是把数据和变量给关联起来. 对变量赋值x = y是把变量 ...

  8. jQuery 开始动画,停止动画

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. VirtualBox中的Linux读取Windows共享目录

    1.安装VirtualBox的增强功能.菜单 -> 设备 -> 安装增强功能此时在Linux中会载入安装包,用管理员权限运行安装即可. 2.在VirtualBox设置共享目录.设置 -&g ...

  10. 管理Fragments

    FragmentManager 为了管理Activity中的fragments,需要使用FragmentManager. 为了得到它,需要调用Activity中的getFragmentManager( ...