【leetcode】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].
Note: Recursive solution is trivial, could you do it iteratively?
例如以下列出三种解法:一种迭代的和两种iterative的
import java.util.ArrayList;
import java.util.List;
import java.util.Stack; public class Binary_Tree_Postorder_Traversal { public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
} public static void main(String[] args) {
TreeNode r1 = new TreeNode(1);
TreeNode r2 = new TreeNode(2);
TreeNode r3 = new TreeNode(3);
TreeNode r4 = new TreeNode(4);
TreeNode r5 = new TreeNode(5);
TreeNode r6 = new TreeNode(6); r1.left = r2;
r1.right = r3;
r2.left = r4;
r2.right = r5;
r3.right = r6; List pr = postorderTraversal1(r1); int size = pr.size(); System.out.println("the size is " + size); for (int i =0 ; i< size; i++) {
System.out.println(pr.get(i));
}
} public static List<Integer> postorderTraversal1(TreeNode root) {
ArrayList<Integer> result = new ArrayList<Integer>(); if (root == null)
return result; result.addAll(postorderTraversal1(root.left));
result.addAll(postorderTraversal1(root.right));
result.add(root.val); return result;
} public static List<Integer> postorderTraversal2(TreeNode root) {
ArrayList<Integer> result = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
Stack<TreeNode> output = new Stack<TreeNode>(); if (root == null) return result; stack.push(root); while(!stack.empty()) {
TreeNode cur = stack.pop();
output.push(cur); if(cur.left != null) {
stack.push(cur.left);
}
if(cur.right != null) {
stack.push(cur.right);
}
} while(!output.isEmpty()) {
result.add(output.pop().val);
}
return result;
} public List<Integer> postorderTraversal3(TreeNode root) {
ArrayList<Integer> result = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode prev = null; // previously traversed node
TreeNode curr = root; if (root == null) {
return result;
} stack.push(root);
while (!stack.empty()) {
curr = stack.peek();
if (prev == null || prev.left == curr || prev.right == curr) { // traverse down the tree
if (curr.left != null) {
stack.push(curr.left);
} else if (curr.right != null) {
stack.push(curr.right);
}
} else if (curr.left == prev) { // traverse up the tree from the left
if (curr.right != null) {
stack.push(curr.right);
}
} else { // traverse up the tree from the right
result.add(curr.val);
stack.pop();
}
prev = curr;
} return result;
}
}
【leetcode】Binary Tree Postorder Traversal的更多相关文章
- 【LeetCode】Binary Tree Postorder Traversal(二叉树的后序遍历)
这道题是LeetCode里的第145道题. 题目要求: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...
- 【leetcode】Binary Tree Postorder Traversal (hard) ☆
二叉树的后序遍历 用标记右子树vector的方法 vector<int> postorderTraversal(TreeNode *root) { vector<int> an ...
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 【LeetCode】Binary Tree Inorder Traversal
Binary Tree Inorder Traversal Total Accepted: 16406 Total Submissions: 47212My Submissions Given a b ...
- 【Leetcode】【hard】Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- 【leetcode】Binary Tree Preorder Traversal (middle)★
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【LeetCode】Binary Tree Inorder Traversal(二叉树的中序遍历)
这道题是LeetCode里的第94道题. 题目要求: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单 ...
- 【LeetCode】Binary Tree Preorder Traversal(二叉树的前序遍历)
这道题是LeetCode里的第144道题. 题目要求: 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很 ...
- 【Leetcode】Binary Tree Traversal
把三个二叉树遍历的题放在一起了. 递归写法太简单,就不再实现了,每题实现了两种非递归算法. 一种是利用栈,时间和空间复杂度都是O(n). 另一种是借助线索二叉树,也叫Morris遍历,充分利用树中节点 ...
随机推荐
- 修改sql server sa用户密码
EXEC sp_password NULL, 'NewPassword', 'Sa'
- KVM-克隆和快照管理
kvm 虚拟机有两部分组成:img镜像文件和xml配置文件 /etc/libvirt/qemu #xml配置文件目录,存在虚拟机所有的详细信息 1.kvm虚拟机克隆 克隆命令 virt-clone - ...
- shadowssock启动服务
启动服务:ssserver -c /var/ss/server.json
- 17-7-24-react入门
先说明下为什么说好每天一更,周五周六周日都没有更新.因为在周五的时候,上司主动找我谈了转正后的工资4-4.5K.本来想好是6K的,后来打听了一圈公司的小伙伴,都是5-5.5,我就把自己定到了5K.万万 ...
- 【读书笔记】周志华《机器学习》第三版课后习题讨<第一章-绪论>
虽然是绪论..但是...真的有点难!不管怎么说,一点点前进吧... 声明一下答案不一定正确,仅供参考,为本人的作答,希望大神们能多多指教~ 1.1 表1.1中若只包含编号为1和4的两个样例,试给出相应 ...
- RMQ入门
注:为方便描述算法 便于记忆 所以ST的代码用Pascal书写 见谅 RMQ,即Range Minimum/Maximum Query问题,给定一个区间,询问不同子区间的最值问题. 当询问次数较少时, ...
- 【数论】bzoj1968 [Ahoi2005]COMMON 约数研究
对于i属于[1,n],i只能成为[1,n]中n/i个数的约数,易证. #include<stdio.h> int n,i; long long ans; int main() { scan ...
- 【动态规划+二分查找】POJ2533&POJ1631最长上升子序列(LIS)
POJ2533裸的LIS,时间复杂度为O(n^2) #include<iostream> #include<cstdio> using namespace std; +; in ...
- python3-开发进阶 heapq模块(如何查找最大或最小的N个元素)
一.怎样从一个集合中获得最大或者最小的 N 个元素列表? heapq 模块有两个函数:nlargest() 和 nsmallest() 可以完美解决这个问题. import heapq nums = ...
- [转] Hibernate与 MyBatis的比较
hibernateHibernateibatisIBATISMyBatismybatis 目录(?)[-] 第一章 Hibernate与MyBatis 1 Hibernate 简介 2 MyB ...