LeetCode OJ --问题与解答 Binary Tree Postorder Traversal
2014-03-28 12:18
477 查看
题目
Given a binary tree, return the postorder traversal
of its nodes' values.
后序遍历,递归、迭代两种方式
思路
1 练习,理解,练习,理解
2 递归很简单
3 迭代后续最难。需要两个stack。网上看到很多种写法,为了还原栈的工作过程,我觉得理解一下是可以的,但是要还原不容易,所以这里选取了一个最容易记忆的方式来写。一看代码,就清楚了。
代码
1 递归
public class Solution {
public ArrayList<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> ans = new ArrayList<Integer>();
useme(root,ans);
return ans;
}
public void useme(TreeNode root, ArrayList<Integer> ans){
if(root!=null){
useme(root.left,ans);
useme(root.right,ans);
ans.add(root.val);
}
}
}
2 迭代
public class Solution {
public ArrayList<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> ans = new ArrayList<Integer>();
LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
LinkedList<TreeNode> output = new LinkedList<TreeNode>();
if(root==null){
return ans;
}
stack.push(root);
while(!stack.isEmpty()){
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()){
TreeNode cur = output.pop();
ans.add(cur.val);
}
return ans;
}
}
Given a binary tree, return the postorder traversal
of its nodes' values.
后序遍历,递归、迭代两种方式
思路
1 练习,理解,练习,理解
2 递归很简单
3 迭代后续最难。需要两个stack。网上看到很多种写法,为了还原栈的工作过程,我觉得理解一下是可以的,但是要还原不容易,所以这里选取了一个最容易记忆的方式来写。一看代码,就清楚了。
代码
1 递归
public class Solution {
public ArrayList<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> ans = new ArrayList<Integer>();
useme(root,ans);
return ans;
}
public void useme(TreeNode root, ArrayList<Integer> ans){
if(root!=null){
useme(root.left,ans);
useme(root.right,ans);
ans.add(root.val);
}
}
}
2 迭代
public class Solution {
public ArrayList<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> ans = new ArrayList<Integer>();
LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
LinkedList<TreeNode> output = new LinkedList<TreeNode>();
if(root==null){
return ans;
}
stack.push(root);
while(!stack.isEmpty()){
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()){
TreeNode cur = output.pop();
ans.add(cur.val);
}
return ans;
}
}
相关文章推荐
- LeetCode OJ --问题与解答 Binary Tree Preorder Traversal
- LeetCode OJ --问题与解答 Binary Tree Preorder Traversal
- LeetCode OJ --问题与解答 Binary Tree Inorder Traversal
- [LeetCode OJ]Binary Tree Postorder Traversal
- LeetCode OJ平台上Binary Tree Postorder Traversal题目用java堆栈实现
- LeetCode OJ--Construct Binary Tree from Inorder and Postorder Traversal *
- LeetCode OJ 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode OJ 之 Binary Tree Postorder Traversal (二叉树的后序遍历)
- LeetCode OJ:Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal (&&Preorder and Inorder Traversal )——数据结构和算法的基本问题
- 【LEETCODE OJ】Binary Tree Postorder Traversal
- LeetCode OJ--Binary Tree Postorder Traversal
- LeetCode OJ:Binary Tree Postorder Traversal
- LeetCode OJ:Construct Binary Tree from Inorder and Postorder Traversal(从中序以及后序遍历结果中构造二叉树)
- [LeetCode OJ] Binary Tree Postorder Traversal 解题报告
- LeetCode OJ: Binary Tree Postorder Traversal
- LeetCode OJ - Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal问题及解法
- 【LeetCode OJ】Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode OJ 106. Construct Binary Tree from Inorder and Postorder Traversal