LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
145. 二叉树的后序遍历
145. Binary Tree Postorder Traversal
题目描述
给定一个二叉树,返回它的 后序 遍历。
LeetCode145. Binary Tree Postorder Traversal
示例:
输入: [1,null,2,3]
1
\
2
/
3
输出: [3,2,1]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
Java 实现
Iterative Solution
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> result = new LinkedList<>();
Stack<TreeNode> stack = new Stack<>();
if (root == null) {
return result;
}
stack.push(root);
while (!stack.isEmpty()) {
TreeNode cur = stack.pop();
result.add(0, cur.val);
if (cur.left != null) {
stack.push(cur.left);
}
if (cur.right != null) {
stack.push(cur.right);
}
}
return result;
}
}
Recursive Solution
import java.util.LinkedList;
import java.util.List;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
class Solution {
List<Integer> result = new LinkedList<>();
public List<Integer> postorderTraversal(TreeNode root) {
if (root == null) {
return result;
}
postorderTraversal(root.left);
postorderTraversal(root.right);
result.add(root.val);
return result;
}
}
相似题目
参考资料
- https://leetcode-cn.com/problems/binary-tree-postorder-traversal/
- https://leetcode.com/problems/binary-tree-postorder-traversal/
LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)的更多相关文章
- [Swift]LeetCode145. 二叉树的后序遍历 | Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
- Java实现 LeetCode 145 二叉树的后序遍历
145. 二叉树的后序遍历 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成 ...
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
题目描述 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 后 ...
- LeetCode 145 二叉树的后序遍历(非递归)
题目: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路: 1 ...
- Leetcode 145. 二叉树的后序遍历
题目链接 https://leetcode-cn.com/problems/binary-tree-postorder-traversal/description/ 题目描述 给定一个二叉树,返回它的 ...
- LeetCode 145. 二叉树的后序遍历 (用栈实现后序遍历二叉树的非递归算法)
题目链接:https://leetcode-cn.com/problems/binary-tree-postorder-traversal/ 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [ ...
- LeetCode 145 ——二叉树的后序遍历
1. 题目 2. 解答 2.1. 递归法 定义一个存放树中数据的向量 data,从根节点开始,如果节点不为空,那么 递归得到其左子树的数据向量 temp,将 temp 合并到 data 中去 递归得到 ...
- 【leetcode 145. 二叉树的后序遍历】解题报告
前往二叉树的:前序,中序,后序 遍历算法 方法一:递归 vector<int> res; vector<int> postorderTraversal(TreeNode* ro ...
随机推荐
- [golang]A modern, fast and scalable websocket framework with elegant API written in Go
A modern, fast and scalable websocket framework with elegant API written in Go http://bit.ly/neffos- ...
- Pytest权威教程21-API参考-02-标记(Marks)
目录 标记(Marks) pytest.mark.filterwarnings pytest.mark.parametrize pytest.mark.skip pytest.mark.skipif ...
- CF1207题解
D 全排列减去坏序列 坏序列分三种,容斥一下就好了 E 比较有意思 \(A=_{i=1}^{100}\{i\},B=_{i=1}^{100}\{i\cdot 2^7\}\),所以\(A_i~xor~ ...
- docker安装postgresql
1.在linux执行以下代码: docker run -p : -v /home/docker/postgresql/data:/var/lib/postgresql/data -e POSTGRES ...
- vmware exsi安装部署
本文章参考:https://blog.csdn.net/fishinhouse/article/details/80980051 1.VMware-ESXi-6.5.0镜像下载 网盘链接:https: ...
- Assignment5: 使用Visual Studio 进行可编码的UI测试
一.实验目的: 使用Visual Studio 可编码的UI测试功能创作自动化测试 二.实验原理:黑盒测试 三.实验需求:win8 app.Visual Studio Ultimate 2012/20 ...
- Tomcat学习四步走:内核、集群、参数及性能
主题简介: 内核实现原理 分布式集群 生产部署关键参数 性能监控和分析 一.内核实现原理 HTTP Web服务器与浏览器之间以HTTP协议通信,浏览器要访问服务器即向服务器发送HTTP请求报文. 如图 ...
- [教程] Packt - Create a Game Environment with Blender and Unity by Darrin Lile
学习了解如何使用Blender,photoshop和Unity创建自己的游戏环境!了解如何通过比以往更加集成的方式使用Blender和Unity,将自己的游戏设计变为现实.在Unity中创建测试版本 ...
- uSurvival 1.41多人在线生存逃杀吃鸡类游戏源码
uSurvival - the new Multiplayer Survival Asset from the creator of uMMORPG. Features:* Kill Zombies ...
- ConsoleWebsocketServer服务端和ConsoleWebsocketClient客户端
本文是简述了Websocket的服务端和客户端的实时通讯过程,Websocket的服务端和客户端的具体使用使用了2种Websocket的服务端和2种客户端. 以下代码使用的是Visual Studio ...