Binary Tree Postorder Traversal Solutions in Java
Number 145
Difficulty Hard
Acceptance 55.1%
Link LeetCode
Solutions
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description//// Author : liuyubobobo/// Time : 2017-11-17import java.util.ArrayList;import java.util.List;// Recursive// Time Complexity: O(n), n is the node number in the tree// Space Complexity: O(h), h is the height of the treepublic class Solution1 {public List<Integer> postorderTraversal(TreeNode root) {ArrayList<Integer> res = new ArrayList<Integer>();postorderTraversal(root, res);return res;}private void postorderTraversal(TreeNode node, List<Integer> list){if(node != null){postorderTraversal(node.left, list);postorderTraversal(node.right, list);list.add(node.val);}}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description//// Author : liuyubobobo/// Time : 2017-11-17import java.util.ArrayList;import java.util.List;import java.util.Stack;// My Non-Recursive// Time Complexity: O(n), n is the node number in the tree// Space Complexity: O(h), h is the height of the treepublic class Solution2 {private class Command{String s; // go, printTreeNode node;Command(String s, TreeNode node){this.s = s;this.node = node;}};public List<Integer> postorderTraversal(TreeNode root) {ArrayList<Integer> res = new ArrayList<Integer>();if(root == null)return res;Stack<Command> stack = new Stack<Command>();stack.push(new Command("go", root));while(!stack.empty()){Command command = stack.pop();if(command.s.equals("print"))res.add(command.node.val);else{assert command.s.equals("go");stack.push(new Command("print", command.node));if(command.node.right != null)stack.push(new Command("go",command.node.right));if(command.node.left != null)stack.push(new Command("go",command.node.left));}}return res;}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description//// Author : liuyubobobo/// Time : 2018-05-30import java.util.ArrayList;import java.util.List;import java.util.Stack;// Non-Recursive// Using a tag to record whether the node has been visited//// Time Complexity: O(n), n is the node number in the tree// Space Complexity: O(h), h is the height of the treepublic class Solution3 {private class TagNode{TreeNode node;boolean isFirst;TagNode(TreeNode node){this.node = node;this.isFirst = false;}};public List<Integer> postorderTraversal(TreeNode root) {ArrayList<Integer> res = new ArrayList<Integer>();if(root == null)return res;Stack<TagNode> stack = new Stack<>();TreeNode cur = root;while(cur != null || !stack.empty()){while(cur != null){stack.push(new TagNode(cur));cur = cur.left;}TagNode tagNode = stack.pop();cur = tagNode.node;if(tagNode.isFirst == false){tagNode.isFirst = true;stack.push(tagNode);cur = cur.right;}else{res.add(cur.val);cur = null;}}return res;}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description//// Author : liuyubobobo/// Time : 2018-05-30import java.util.ArrayList;import java.util.List;import java.util.Stack;// Non-Recursive// Using two stacks, Reverse the Preorder Traversal!//// Time Complexity: O(n)// Space Complexity: O(n)public class Solution4 {public List<Integer> postorderTraversal(TreeNode root) {ArrayList<Integer> res = new ArrayList<Integer>();if(root == null)return res;Stack<TreeNode> stack = new Stack<>();Stack<Integer> output = new Stack<>();stack.push(root);while(!stack.empty()){TreeNode cur = stack.pop();output.push(cur.val);if(cur.left != null)stack.push(cur.left);if(cur.right != null)stack.push(cur.right);}while(!output.empty())res.add(output.pop());return res;}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description//// Author : liuyubobobo/// Time : 2018-05-30import java.util.ArrayList;import java.util.List;import java.util.Stack;import java.util.LinkedList;// Non-Recursive// Using two stacks, Reverse the Preorder Traversal!//// Time Complexity: O(n)// Space Complexity: O(n)public class Solution5 {public List<Integer> postorderTraversal(TreeNode root){Stack<TreeNode> stack = new Stack<>();LinkedList<TreeNode> output = new LinkedList<>();TreeNode p = root;while(p != null || !stack.isEmpty()){if(p != null){stack.push(p);output.push(p);p = p.right;}else{p = stack.pop();p = p.left;}}List<Integer> res = new ArrayList<>();while(!output.isEmpty())res.add(output.pop().val);return res;}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description//// Author : liuyubobobo/// Time : 2018-05-31import java.util.ArrayList;import java.util.List;import java.util.Stack;// Non-Recursive// Using a pre pointer to record the last visted node//// Time Complexity: O(n)// Space Complexity: O(h)public class Solution6 {public List<Integer> postorderTraversal(TreeNode root) {ArrayList<Integer> res = new ArrayList<Integer>();if(root == null)return res;Stack<TreeNode> stack = new Stack<>();TreeNode pre = null;stack.push(root);while(!stack.empty()){TreeNode cur = stack.pop();if((cur.left == null && cur.right == null) ||(pre != null && pre == cur.left && cur.right == null) ||(pre != null && pre == cur.right)){res.add(cur.val);pre = cur;}else{stack.push(cur);if(cur.right != null)stack.push(cur.right);if(cur.left != null)stack.push(cur.left);}}return res;}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description//// Author : liuyubobobo/// Time : 2018-05-31import java.util.ArrayList;import java.util.List;import java.util.Stack;// Classic Non-Recursive// Using a pre pointer to record the last visted node//// Time Complexity: O(n)// Space Complexity: O(h)public class Solution7 {public List<Integer> postorderTraversal(TreeNode root) {ArrayList<Integer> res = new ArrayList<Integer>();if(root == null)return res;Stack<TreeNode> stack = new Stack<>();TreeNode pre = null;TreeNode cur = root;while(cur != null || !stack.empty()){while(cur != null){stack.push(cur);cur = cur.left;}cur = stack.pop();if(cur.right == null || pre == cur.right){res.add(cur.val);pre = cur;cur = null;}else{stack.push(cur);cur = cur.right;}}return res;}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description//// Author : liuyubobobo/// Time : 2018-05-31import java.util.ArrayList;import java.util.List;import java.util.Stack;// Classic Non-Recursive// Using a pre pointer to record the last visted node//// Time Complexity: O(n)// Space Complexity: O(h)public class Solution8 {public List<Integer> postorderTraversal(TreeNode root) {ArrayList<Integer> res = new ArrayList<Integer>();if(root == null)return res;Stack<TreeNode> stack = new Stack<>();TreeNode pre = null;TreeNode cur = root;while(cur != null || !stack.empty()){if(cur != null){stack.push(cur);cur = cur.left;}else{cur = stack.pop();if(cur.right == null || pre == cur.right){res.add(cur.val);pre = cur;cur = null;}else{stack.push(cur);cur = cur.right;}}}return res;}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description//// Author : liuyubobobo/// Time : 2018-05-31import java.util.ArrayList;import java.util.Collections;import java.util.List;import java.util.Stack;// Morris PostOrder Traversal//// Time Complexity: O(n)// Space Complexity: O(1)public class Solution9 {public List<Integer> postorderTraversal(TreeNode root) {ArrayList<Integer> res = new ArrayList<Integer>();if(root == null)return res;TreeNode dummyRoot = new TreeNode(-1);dummyRoot.left = root;TreeNode cur = dummyRoot;while(cur != null){if(cur.left == null)cur = cur.right;else{TreeNode pre = cur.left;while(pre.right != null && pre.right != cur)pre = pre.right;if(pre.right == null){pre.right = cur;cur = cur.left;}else{pre.right = null;reverseTraversal(cur.left, res);cur = cur.right;}}}return res;}private void reverseTraversal(TreeNode node, ArrayList<Integer> res){int start = res.size();while(node != null){res.add(node.val);node = node.right;}int i = start, j = res.size() - 1;while(i < j){Integer t = res.get(i);res.set(i, res.get(j));res.set(j, t);i ++;j --;}}}