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

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

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

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

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

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

代码:

  1. class Solution {
  2. public:
  3. vector<int> postorderTraversal(TreeNode *root) {
  4. vector<int> res;
  5. if(root==NULL) return res;
  6. stack<pair<TreeNode*,int>> s;
  7. int unUsed;
  8. s.push(make_pair(root,));
  9.  
  10. while (!s.empty())
  11. {
  12. root=s.top().first;
  13. unUsed=s.top().second;
  14. s.pop();
  15. if(unUsed){
  16. s.push(make_pair(root,));
  17. if(root->right)
  18. s.push(make_pair(root->right,));
  19. if(root->left)
  20. s.push(make_pair(root->left,));
  21. }else{
  22. //cout<<root->val<<" ";
  23. res.push_back(root->val);
  24. }
  25. }
  26. }
  27. };

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

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

代码:

  1. void inOrder(Node *p)
  2. {
  3. if(!p)
  4. return;
  5. stack< pair<Node*,int> > s;
  6. Node *t;
  7. int unUsed;
  8. s.push(make_pair(p,));
  9. while(!s.empty())
  10. {
  11. t=s.top().first;
  12. unUsed = s.top().second;
  13. s.pop();
  14. if(unUsed)
  15. {
  16. if(t->right)
  17. s.push( make_pair(t->right,) );
  18. s.push( make_pair(t,) );
  19. if(t->left)
  20. s.push( make_pair(t->left,));
  21. }
  22. else printf("%d\n",t->data);
  23. }
  24. }

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

  1. void preOrder(Node *p) //非递归
  2. {
  3. if(!p) return;
  4. stack<Node*> s;
  5. Node *t;
  6. s.push(p);
  7. while(!s.empty())
  8. {
  9. t=s.top();
  10. printf("%d\n",t->data);
  11. s.pop();
  12. if(t->right) s.push(t->right);
  13. if(t->left) s.push(t->left);
  14. }
  15. }

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. LN : leetcode 646 Maximum Length of Pair Chain

    lc 646 Maximum Length of Pair Chain 646 Maximum Length of Pair Chain You are given n pairs of number ...

  2. Heart Beat

    实现关键: 1.纯css实现心形图(如果使用图片则无需) html代码: <!DOCTYPE html> <html> <head> <meta charse ...

  3. Elasticsearch--集群管理_再平衡&预热

    目录 控制集群的再平衡 再平衡 集群的就绪 集群再平衡设置 控制再平衡何时开始 控制同时在节点移动的分片数量 控制单个节点上同时初始化的分片数量 控制单个节点上同时初始化的主分片数量 控制分配的分片类 ...

  4. Java编程思想总结笔记Chapter 5

    初始化和清理是涉及安全的两个问题.本章简单的介绍“垃圾回收器”及初始化知识. 第五章  初始化与清理 目录:5.1 用构造器确保初始化5.2 方法重载5.3 默认构造器5.4 this关键字5.5 清 ...

  5. Python学习 Day 1-简介 安装 Hello world

    简介 Python(英语发音:/ˈpaɪθən/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年,Python 源 ...

  6. sql语句中截取字符串

    今天在开发过程中因为要用到合并单元格,在程序里实现了以后,查出来的数据太长,都把格式撑大了,后来想想可以在sql语句查询的时候就截取,就去网上找了一下,挺好用,就转了过来: 合并单元格: /// &l ...

  7. Vue 路由知识三(过渡动画及路由钩子函数)

    路由的过渡动画:让路由有过渡动画,需要在<router-view>标签的外部添加<transition>标签,标签还需要一个name属性. <transition nam ...

  8. leetcode_894. All Possible Full Binary Trees

    https://leetcode.com/problems/all-possible-full-binary-trees/ 给定节点个数,求所有可能二叉树,该二叉树所有节点要么有0个子节点要么有两个子 ...

  9. 生产者-消费者中的缓冲区:BlockingQueue接口

    BlockingQueue接口使用场景相信大家对生产者-消费者模式不陌生,这个经典的多线程协作模式,最简单的描述就是生产者线程往内存缓冲区中提交任务,消费者线程从内存缓冲区里获取任务执行.在生产者-消 ...

  10. charsets - 程序员对字符集和国际化的观点

    描述 Linux 是一个国际性的操作系统.它的各种各样实用程序和设备驱动程序 (包括控制台驱动程序 ) 支持多种语言的字符集,包括带有附加符号的拉丁字母表字符,重音符,连字(字母结合), 和全部非拉丁 ...