LeetCode 145 Binary Tree Postorder Traversal (后序遍历二叉树)
2016-08-31 10:02
399 查看
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree
return
Note: Recursive solution is trivial, could you do it iteratively?
题目链接:https://leetcode.com/problems/binary-tree-postorder-traversal/
递归:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public void DFS(TreeNode root, List<Integer> ans) {
if(root == null) {
return;
}
DFS(root.left, ans);
DFS(root.right, ans);
ans.add(root.val);
}
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> ans = new ArrayList<>();
DFS(root, ans);
return ans;
}
}
非递归:后序遍历左右中,考虑到栈的先进先出,故先将右子树压栈,两种情况记录答案出栈,1是遇到叶子,2是父节点的子节点已经被访问过,根据压栈顺序,可以保证这样可以完成后序遍历
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> ans = new ArrayList<>();
Stack<TreeNode> stk = new Stack<>();
if(root != null) {
stk.push(root);
}
TreeNode cur = null;
TreeNode pre = null;
while(!stk.empty()) {
cur = stk.peek();
if(cur.left == null && cur.right == null) {
ans.add(cur.val);
pre = cur;
stk.pop();
}
else if(pre != null && (cur.left == pre || cur.right == pre)) {
ans.add(cur.val);
pre = cur;
stk.pop();
}
else {
if(cur.right != null) {
stk.push(cur.right);
}
if(cur.left != null) {
stk.push(cur.left);
}
}
}
return ans;
}
}
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?
题目链接:https://leetcode.com/problems/binary-tree-postorder-traversal/
递归:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public void DFS(TreeNode root, List<Integer> ans) {
if(root == null) {
return;
}
DFS(root.left, ans);
DFS(root.right, ans);
ans.add(root.val);
}
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> ans = new ArrayList<>();
DFS(root, ans);
return ans;
}
}
非递归:后序遍历左右中,考虑到栈的先进先出,故先将右子树压栈,两种情况记录答案出栈,1是遇到叶子,2是父节点的子节点已经被访问过,根据压栈顺序,可以保证这样可以完成后序遍历
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> ans = new ArrayList<>();
Stack<TreeNode> stk = new Stack<>();
if(root != null) {
stk.push(root);
}
TreeNode cur = null;
TreeNode pre = null;
while(!stk.empty()) {
cur = stk.peek();
if(cur.left == null && cur.right == null) {
ans.add(cur.val);
pre = cur;
stk.pop();
}
else if(pre != null && (cur.left == pre || cur.right == pre)) {
ans.add(cur.val);
pre = cur;
stk.pop();
}
else {
if(cur.right != null) {
stk.push(cur.right);
}
if(cur.left != null) {
stk.push(cur.left);
}
}
}
return ans;
}
}
相关文章推荐
- LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
- LeetCode Binary Tree Postorder Traversal(二叉树的后序遍历 非递归实现)
- 【LeetCode】Binary Tree Postorder Traversal 二叉树的后序遍历 - Easy
- 【LeetCode笔记】Construct Binary Tree from Inorder and Postorder Traversal 中序、后序遍历求二叉树
- LeetCode: 106_Construct Binary Tree from Inorder and Postorder Traversal | 根据中序和后序遍历构建二叉树 | Medium
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal (用中序和后序树遍历来建立二叉树)
- [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
- LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)
- LeetCode 145:Binary Tree Postorder Traversal(后序遍历)
- Leetcode - Tree - 106. Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历重构二叉树)
- LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)
- LeetCode-106:Construct Binary Tree from Inorder and Postorder Traversal (利用中序和后序遍历构建二叉树) -- medium
- Leetcode Construct Binary Tree from Inorder and Postorder Traversal 中序后序遍历重组二叉树
- Construct Binary Tree from Inorder and Postorder Traversal ——通过中序、后序遍历得到二叉树
- 二叉树的后序遍历 Binary Tree Postorder Traversal
- LeetCode OJ:Construct Binary Tree from Inorder and Postorder Traversal(从中序以及后序遍历结果中构造二叉树)
- LeetCode OJ:Binary Tree Postorder Traversal(后序遍历二叉树)
- Construct Binary Tree from Inorder and Postorder Traversal 中序和后序遍历二叉树
- lintcode 容易题:Binary Tree Postorder Traversal 二叉树的后序遍历