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

For example:
Given binary tree [1,null,2,3],

   1\2/3

思路: 借助于一个栈,依次将根节点的右子节点和左子节点压入栈中。如果一个节点为叶子节点,或者前一个出栈的元素为当前栈顶节点的子节点,则出栈。
 vector<int> postorderTraversal(TreeNode* root) {vector<int> res;stack<TreeNode*> _stack;TreeNode *cur=root;TreeNode *pre=NULL;if(cur!=NULL)_stack.push(cur);while(!_stack.empty()){cur=_stack.top();if((cur->left==NULL&&cur->right==NULL)||(pre&&(cur->left==pre||cur->right==pre))){res.push_back(cur->val);pre=cur;_stack.pop();}else{if(cur->right)_stack.push(cur->right);if(cur->left)_stack.push(cur->left);}}return res;}

转载于:https://www.cnblogs.com/zhaoyaxing/p/8502683.html

145. Binary Tree Postorder Traversal相关推荐

  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. 145. Binary Tree Postorder Traversal 二叉树的后序遍历

    给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1\2/3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? DFS 今天这道题比较简单,直 ...

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

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

  6. 5. Binary Tree Postorder Traversal

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

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

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

  8. Binary Tree Postorder Traversal

    https://leetcode.com/problems/binary-tree-postorder-traversal/ Given a binary tree, return the posto ...

  9. Leetcode 之Binary Tree Postorder Traversal(44)

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

最新文章

  1. Chapter 2 Open Book——29
  2. 如何使用 Node.js 访问 SAP HANA Cloud 数据库里的数据
  3. STM32的串口通信
  4. java质数判断程序_java判断一个数是否为素数/质数
  5. python 进程池、线程池 与异步调用、回调机制
  6. oracle chinese_china.al32utf8,Oracle11g字符集更改为AL32UTF8
  7. Power BI for Office 365 概览
  8. dw怎么打开html模板,Dreamweaver怎样使用网页模板及修改模板
  9. DOS原理和常用命令详解示例
  10. windows10怎么卸载计算机,Win10怎么卸载声卡驱动?电脑声卡驱动怎么卸载?
  11. supermap地图展示OD图
  12. 开发一个APP系统软件需要多少钱及需要多长开发周期
  13. 7段数码管动态扫描c语言,7段数码管动态扫描原理及其实现方式
  14. IP地址-子网掩码-缺省网关
  15. 高频故障-桌面图标变成白纸图标的恢复方案
  16. 测试第一步,雀氏纸尿裤
  17. My设置密码报错:ERROR 1819 (HY000): Your password does not satisfy the current policy requir
  18. 不能在具有唯一索引 ‘NonClusteredIndex-20220419-093301‘ 的对象 ‘dbo.cux_machine_info‘ 中插入重复键的行。 语句已终止。
  19. 微信怎么收银行卡的消息服务器,你的银行卡短信通知收费吗
  20. 计算机网络经典选择题20道

热门文章

  1. 机器学习算法总结--提升方法
  2. Registered Nurse in the US
  3. 如何给APK文件签名
  4. python考试有什么用_Python有什么用?2020年学习Python的10个理由
  5. 面试必备:多线程学习(一)
  6. apollo报:系统出错,请重试或联系系统负责人
  7. Android 底部上拉抽屉view
  8. java 字符串数组转int数组_java怎么把字符型数组转换为int型?
  9. JAVA8 ARRAY、LIST操作 汇【5】)- JAVA8 LAMBDA LIST统计(求和、最大、最小、平均)
  10. 数据结构实验三 树的遍历生成树