【LeetCode】145. Binary Tree Postorder Traversal
Difficulty: Hard
More:【目录】LeetCode Java实现
Description
https://leetcode.com/problems/binary-tree-postorder-traversal/
Given a binary tree, return the postordertraversal of its nodes' values.
Example:
Input:[1,null,2,3]
1
\
2
/
3 Output:[3,2,1]
Follow up: Recursive solution is trivial, could you do it iteratively?
Intuition
Method 1. Using one stack to store nodes, and another to store a flag wheather the node has traverse right subtree.
Method 2. Stack + Collections.reverse( list )
Solution
Method 1
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new LinkedList<Integer>();
Stack<TreeNode> nodeStk = new Stack<TreeNode>();
Stack<Boolean> tag = new Stack<Boolean>();
while(root!=null || !nodeStk.isEmpty()){
while(root!=null){
nodeStk.push(root);
tag.push(false);
root=root.left;
}
if(!tag.peek()){
tag.pop();
tag.push(true);
root=nodeStk.peek().right;
}else{
list.add(nodeStk.pop().val);
tag.pop();
}
}
return list;
}
Method 2
public List<Integer> postorderTraversal(TreeNode root) {
LinkedList<Integer> list = new LinkedList<Integer>();
Stack<TreeNode> stk = new Stack<>();
stk.push(root);
while(!stk.isEmpty()){
TreeNode node = stk.pop();
if(node==null)
continue;
list.addFirst(node.val); //LinkedList's method. If using ArrayList here,then using 'Collections.reverse(list)' in the end;
stk.push(node.left);
stk.push(node.right);
}
return list;
}
Complexity
Time complexity : O(n)
Space complexity : O(nlogn)
What I've learned
1. linkedList.addFirst( e )
2. Collections.reverse( arraylist )
More:【目录】LeetCode Java实现
【LeetCode】145. Binary Tree Postorder Traversal的更多相关文章
- 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 【LeetCode】94. Binary Tree Inorder Traversal (3 solutions)
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- LeetCode OJ 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- 【LeetCode】144. Binary Tree Preorder Traversal 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- 【LeetCode】590. N-ary Tree Postorder Traversal 解题报告 (C++&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 相似题目 参考资料 日期 题目地址:htt ...
- 【LeetCode】94. Binary Tree Inorder Traversal
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...
随机推荐
- FCC-学习笔记 Convert HTML Entities
FCC-学习笔记 Convert HTML Entities 1>最近在学习和练习FCC的题目.这个真的比较的好,推荐给大家. 2>中文版的地址:https://www.freecode ...
- map、filter、reduce函数的使用
1.filter() 作用:过滤 // 1.筛选出大于30的数. const array = [10, 20, 30, 40, 50, 60, 70, 80] // 普通写法 // let newar ...
- leetcode - 链表两两元素交换 + 判断链表有无环
链表两两元素交换 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2->3->4, 你 ...
- 004-OpenStack-计算服务
OpenStack-计算服务 [基于此文章的环境]点我快速打开文章 1.控制节点(controller) 1.1 创库授权 nova_api, nova, 和 nova_cell0 mysql CR ...
- 笔记4:WEB服务器
web服务器 1 HTTP协议 http:超文本传输协议,基于tcp的方式,会更稳当更安全.协议就是规定了怎样去请求服务器,服务器如何返回信息.如下图红色方框标记所示: 打开浏览器电商广告原理: 我们 ...
- Maven 中 dependencyManagement 元素,知识点
Maven 提供的 dependencyManagement 元素既能让子模块继承到父模块的依赖配置,又能保证子模块依赖使用的灵活性.在 dependencyManagement 元素下的依赖声明不会 ...
- Android Adapter中获得LayoutInflater
LayoutInflater li =(LayoutInflater)MyContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- LeetCode 112. Path Sum路径总和 (C++)
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- USACO Stamps
洛谷 P2725 邮票 Stamps https://www.luogu.org/problem/P2725 JDOJ 1797: Stamps 邮票 https://neooj.com:8082/o ...
- scrapy机制mark(基于twisted)
twisted twisted管理了所有的异步任务 Twisted的主线程是单线程的,即reactor线程: 而这些io耗时操作会在线程池中运行,不再twisted主线程中运行,即通过线程池来执行异步 ...