145.Binary Tree Postorder Traversal
题目:
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相关推荐
- 【二叉树的迭代版后序遍历】LeetCode 145. Binary Tree Postorder Traversal
LeetCode 145. Binary Tree Postorder Traversal Solution1:递归版答案 二叉树的后序遍历递归版是很简单的,关键是迭代版的代码既难理解又难写!但听了花 ...
- LeetCode 145. Binary Tree Postorder Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-postorder-traversal/ 题目: Given a binary tree, retu ...
- 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- LeetCode:145. Binary Tree Postorder Traversal
总共想出了四种方法来做. 第一种是递归,不说了.后面集中都是迭代. 第二种是使用一个栈,而且借助一个set来记录哪些node是访问过它的子节点的,防止重复访问.缺点是需要额外的set的空间 class ...
- 145. Binary Tree Postorder Traversal 二叉树的后序遍历
给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1\2/3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? DFS 今天这道题比较简单,直 ...
- LeetCode 145. Binary Tree Postorder Traversal--后序遍历--先序遍历反向输出--递归,迭代--C++,Python解法
题目地址:Binary Tree Postorder Traversal - LeetCode Given a binary tree, return the postorder traversal ...
- 5. Binary Tree Postorder Traversal
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- Binary Tree Postorder Traversal
https://leetcode.com/problems/binary-tree-postorder-traversal/ Given a binary tree, return the posto ...
- Leetcode 之Binary Tree Postorder Traversal(44)
后序遍历,比先序和中序都要复杂.访问一个结点前,需要先判断其右孩子是否被访问过.如果是,则可以访问该结点:否则,需要先处理右子树. vector<int> postorderTravers ...
最新文章
- Transformer 架构逐层功能介绍和详细解释
- OpenCV-Python 雪花飘落特效
- 11部高分学科纪录片,助力孩子涨姿势拓视野~
- 扩展Java EE应用程序的基础
- linux中 /dev/null命令
- Python3 内建模块 hashlib、itertools、HTMLParser、urllib
- 深度解读MRS IoTDB时序数据库的整体架构设计与实现
- Excel表复制、取消工作表保护、解除冻结操作
- PSpice电路模拟
- 学成在线项目学习前端bug npm run dev运行报错 error in ./src/App.vue error in ./src/components/HelloWorld.vue问题
- IIS安装及ASPNet开发环境配置
- 禁忌搜索算法(Tabu Search)的基本原理与算法流程总结
- TongLinkQ消息中间件使用(c语言)
- FOC和SVPWM的C语言代码实现
- 无线学习之mac80211专题-1-自适应速率控制算法
- git删除分支时究竟会删掉哪些东西
- 3dmax的fbx文件如何导入unity使用?
- android手机与电脑无线连接工具handshaker
- 产品随记-无埋点数据采集
- docker 容器常用命令及基本操作