后序遍历,比先序和中序都要复杂。访问一个结点前,需要先判断其右孩子是否被访问过。如果是,则可以访问该结点;否则,需要先处理右子树。

 vector<int> postorderTraversal(TreeNode *root){vector<int> result;stack<TreeNode *>s;TreeNode *p, *q;//一个表示当前访问的结点,一个表示刚刚访问过的结点p = root;do{while (p != nullptr){//不断将左结点压入
                  s.push(p);p = p->left;}q = nullptr;//压到底时,刚刚访问过的结点必定为空结点while (!s.empty()){p = s.top();s.pop();if (p->right == q){//如果当前结点的右结点已经被访问,则访问该结点result.push_back(p->val);q = p;}else{//右孩子没有被访问过,则继续压入,先处理右子树
                      s.push(p);p = p->right;break;}}} while (!s.empty());}

View Code

转载于:https://www.cnblogs.com/573177885qq/p/5537677.html

Leetcode 之Binary Tree Postorder Traversal(44)相关推荐

  1. 【二叉树的迭代版后序遍历】LeetCode 145. Binary Tree Postorder Traversal

    LeetCode 145. Binary Tree Postorder Traversal Solution1:递归版答案 二叉树的后序遍历递归版是很简单的,关键是迭代版的代码既难理解又难写!但听了花 ...

  2. LeetCode 145. Binary Tree Postorder Traversal

    原题链接在这里:https://leetcode.com/problems/binary-tree-postorder-traversal/ 题目: Given a binary tree, retu ...

  3. LeetCode:145. Binary Tree Postorder Traversal

    总共想出了四种方法来做. 第一种是递归,不说了.后面集中都是迭代. 第二种是使用一个栈,而且借助一个set来记录哪些node是访问过它的子节点的,防止重复访问.缺点是需要额外的set的空间 class ...

  4. LeetCode 145. Binary Tree Postorder Traversal--后序遍历--先序遍历反向输出--递归,迭代--C++,Python解法

    题目地址:Binary Tree Postorder Traversal - LeetCode Given a binary tree, return the postorder traversal ...

  5. 5. Binary Tree Postorder Traversal

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

  6. 【二叉树迭代版中序遍历】LeetCode 94. Binary Tree Inorder Traversal

    LeetCode 94. Binary Tree Inorder Traversal Solution1:递归版 二叉树的中序遍历递归版是很简单的,中序遍历的迭代版需要特殊记一下! 迭代版链接:htt ...

  7. 【二叉树迭代版前序遍历】LeetCode 144. Binary Tree Preorder Traversal

    LeetCode 144. Binary Tree Preorder Traversal Solution1:递归版 二叉树的前序遍历递归版是很简单的,前序遍历的迭代版相对是最容易理解的. 迭代版链接 ...

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

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

  9. [Lintcode]66. Binary Tree Preorder Traversal/[Leetcode]144. Binary Tree Preorder Traversal

    66. Binary Tree Preorder Traversal/144. Binary Tree Preorder Traversal 本题难度: Easy/Medium Topic: Bina ...

最新文章

  1. windows7下java配置环境
  2. 21个Transformer面试题的简单回答
  3. CSS_伪元素_伪类
  4. Fragment专辑(一):Fragment简介
  5. 页面升紧急级访问_部落冲突10-13本 发育规划丨升什么性价比最高?
  6. Python之钉钉机器人推送天气预报
  7. grace hopper_开源日,在Grace Hopper会议上建立FOSS技能
  8. UVA454 Anagrams【set+排序】
  9. 92. php 命名空间(2)
  10. mysql 月份去0_mysql 查询每个月发布的文章数。月份无数据显示为0。如何写SQL呢...
  11. 【算法】如何将一个文件分割成多份(C,Java语言实现)
  12. RabbitMQ的优势和劣势
  13. 软件架构模式之管道-过滤器模式--分析
  14. PSNRSSIMVIF指标-python版本
  15. 单片机毕业设计 stm32万能红外遥控器
  16. 转载 GIS地图知识
  17. 2019-12-16-全球土地覆盖数据下载源
  18. 《人月神话》第十一弹
  19. 介绍几款最优秀的CMS(网站内容管理系统)
  20. 计算机操作系统论文,计算机操作系统毕业论文

热门文章

  1. Windows Mobile 5.0 设备的目录变化
  2. 小米4c怎么添加语言怎么设置在哪里,小米4c路由器设置教程
  3. 如何在Visual Studio Code中编译C ++代码
  4. python描述器做权限控制_Python装饰器14-描述器
  5. (C++)1027 打印沙漏
  6. (C++)1015 德才论
  7. python邮件发送哪个好_最全总结!聊聊 Python 发送邮件的几种方式
  8. 营销自动化为什么能吸引企业的喜欢?它有何魅力?
  9. mac系统添加VSCode到右键菜单(转)
  10. MFC界面库BCGControlBar v25.3新版亮点:Gauge Controls