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

For example:
Given binary tree {1,#,2,3},
1

\2
/

3
return [3,2,1].

解答:
最主要的思想是先存root的话,整个存储的顺序会变反,所以要插入存储进去。
1.Iterative解答:

public class Solution {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> result = new ArrayList<Integer>();Stack<TreeNode> stack = new Stack<TreeNode>();if (root == null) return result;stack.push(root);while (!stack.isEmpty()) {TreeNode node = stack.pop();result.add(0, node.val);if (node.left != null) stack.push(node.left);if (node.right != null) stack.push(node.right);}return result;}
}

2.Recursive解答:

//Recursive
public void Helper(TreeNode root, List<Integer> result) {if (root == null) return;result.add(0, root.val);Helper(root.right, result);Helper(root.left, result);
}public List<Integer> postorderTraversal(TreeNode root) {List<Integer> result = new ArrayList<Integer>();Helper(root, result);return result;
}

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. 145. Binary Tree Postorder Traversal

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

  4. LeetCode:145. Binary Tree Postorder Traversal

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

  5. 145. Binary Tree Postorder Traversal 二叉树的后序遍历

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

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

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

  7. 5. Binary Tree Postorder Traversal

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

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

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

  9. Binary Tree Postorder Traversal

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

  10. Leetcode 之Binary Tree Postorder Traversal(44)

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

最新文章

  1. Transformer 架构逐层功能介绍和详细解释
  2. OpenCV-Python 雪花飘落特效
  3. 11部高分学科纪录片,助力孩子涨姿势拓视野~
  4. 扩展Java EE应用程序的基础
  5. linux中 /dev/null命令
  6. Python3 内建模块 hashlib、itertools、HTMLParser、urllib
  7. 深度解读MRS IoTDB时序数据库的整体架构设计与实现
  8. Excel表复制、取消工作表保护、解除冻结操作
  9. PSpice电路模拟
  10. 学成在线项目学习前端bug npm run dev运行报错 error in ./src/App.vue error in ./src/components/HelloWorld.vue问题
  11. IIS安装及ASPNet开发环境配置
  12. 禁忌搜索算法(Tabu Search)的基本原理与算法流程总结
  13. TongLinkQ消息中间件使用(c语言)
  14. FOC和SVPWM的C语言代码实现
  15. 无线学习之mac80211专题-1-自适应速率控制算法
  16. git删除分支时究竟会删掉哪些东西
  17. 3dmax的fbx文件如何导入unity使用?
  18. android手机与电脑无线连接工具handshaker
  19. 产品随记-无埋点数据采集
  20. docker 容器常用命令及基本操作

热门文章

  1. Python中去除字符串中空格的方法
  2. 快速搭建一个本地FTP服务器
  3. python执行方式及变量
  4. Filter过滤器处理JSP中文乱码
  5. 《Entity Framework 6 Recipes》中文翻译系列 (11) -----第三章 查询之异步查询 (转)...
  6. Linux挂载windos共享提示error
  7. unity3d 射击游戏BOSS行为代码
  8. MySql中时间类型总结
  9. python基础系列二:列表
  10. 物流货代公司管理系统