LeetCode:二叉树的后序遍历【145】
LeetCode:二叉树的后序遍历【145】
题目描述
给定一个二叉树,返回它的 后序 遍历。
示例:
输入: [1,null,2,3]
1
\
2
/
3 输出: [3,2,1]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
题目分析
这道题难度和N叉树的后序遍历是等同的,但是标注为困难。
首先我们都知道,栈顶元素一般都是根元素,弹出根元素,加入根元素的左节点、右节点后,位于栈顶的是根元素的最右子节点,栈低的是根元素的最左子节点。
如果我们按照这个顺序打印的话,输出的是[1,2,3]。如果我们把这个结果反转后就是[3,2,1]就是结果。
Java题解
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
Stack<TreeNode> stack = new Stack<>();
List<Integer> ans = new ArrayList<>();
if(root==null)
return ans;
stack.push(root);
while (!stack.isEmpty())
{
TreeNode node = stack.pop();
if(node.left!=null)
stack.add(node.left);
if(node.right!=null)
stack.add(node.right);
ans.add(node.val);
}
Collections.reverse(ans);
return ans ;
}
}
LeetCode:二叉树的后序遍历【145】的更多相关文章
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- Java实现 LeetCode 145 二叉树的后序遍历
145. 二叉树的后序遍历 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成 ...
- 【LeetCode】145. 二叉树的后序遍历
145. 二叉树的后序遍历 知识点:二叉树:递归:Morris遍历 题目描述 给定一个二叉树的根节点 root ,返回它的 后序 遍历. 示例 输入: [1,null,2,3] 1 \ 2 / 3 输 ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历
题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...
- LintCode-68.二叉树的后序遍历
二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 返回 [3,2,1] 挑战 你能使用非递归实现么? 标签 递归 二叉树 二叉树遍历 code / ...
- PTA L2-006 树的遍历-二叉树的后序遍历+中序遍历,输出层序遍历 团体程序设计天梯赛-练习集
L2-006 树的遍历(25 分) 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤),是二叉树中结点的 ...
- leecode刷题(30)-- 二叉树的后序遍历
leecode刷题(30)-- 二叉树的后序遍历 二叉树的后序遍历 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 思路 ...
随机推荐
- 简单的SpringCloud 熔断Hystrix
pom配置 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> ...
- pl/sql 实例解析 01
1. 合并 firstname, lastname. 1: declare 2: v_first_name varchar2(255); 3: v_last_name varchar2(255); 4 ...
- list<> 中find的使用
昨天要在 std::list<std::string> 中判断是否存在某一字符串 std::string . 我首先想到的是 list迭代+std::string重载的"==&q ...
- storm 入门原理介绍_AboutYUN
转自:http://www.aboutyun.com/thread-7394-1-1.html 了解Storm:http://www.aboutyun.com/thread-9547-1-2.html ...
- ArrayList和Vector的区别?
ArrayList和Vector的区别? 解答:同步性:Vector是线程安全的,也就是说是同步的,而ArrayList是线程不安全的,不是同步的:数据增长:当需要增长时,Vector默认增长为原来一 ...
- wpf的MVVM框架
http://www.cnblogs.com/KnightsWarrior/archive/2010/11/01/1866641.html 框架名字的介绍在 文章的后面
- jQuery ajax 动态append创建表格出现不兼容ie8
非常多情况下.通过js(jQuery1.10)动态来创建一些样式,对页面的动态交互来说是非常方便的 可是不同的浏览器针对动态生成的不是非常兼容,在此遇见的不兼容ie8.跟各位分享下 代码: json数 ...
- win 8升级win8.1的几个问题
对于习惯了win7来说的小朋友,win8的使用简直有种想砸电脑的冲动啊,特别是玩游戏的小朋友,win8各种bug.对于游戏.咳咳,我只是多说了,我们都是爱学习的好孩子,少玩游戏,少撸.简单说一 ...
- ORA-12505, TNS:listener does not currently know of SID given in connect desc
数据库名(数据库服务名)配置错误.
- shop++源码反编译----随笔
一.applicationContext-mvc.xml配置 1.读取配置文件 <context:property-placeholder location="classpath*:/ ...