Binary Tree Preorder Traversal Solutions in Java
Number 144
Difficulty Medium
Acceptance 55.8%
Link LeetCode
Solutions
Java solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/binary-tree-preorder-traversal/// Inspired by : http://www.jiuzhang.com/solutions/binary-tree-preorder-traversal/// Author : Lei Cao// Date : 2015-10-05package binaryTreePreorderTraversal;import java.util.ArrayList;import java.util.List;/*** Created by leicao on 5/10/15.*/public class binaryTreePreorderTraversal {//Version 1: Traversepublic List<Integer> preorderTraversal(TreeNode root) {List<Integer> results = new ArrayList<Integer>();traversal(results, root);return results;}private void traversal(List<Integer> results, TreeNode root) {if (results == null || root == null) {return;}results.add(root.val);traversal(results, root.left);traversal(results, root.right);}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-preorder-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> preorderTraversal(TreeNode root) {ArrayList<Integer> res = new ArrayList<Integer>();preorderTraversal(root, res);return res;}private void preorderTraversal(TreeNode node, List<Integer> list){if(node != null){list.add(node.val);preorderTraversal(node.left, list);preorderTraversal(node.right, list);}}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-preorder-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> preorderTraversal(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");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));stack.push(new Command("print", command.node));}}return res;}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description//// Author : liuyubobobo/// Time : 2017-11-17import java.util.ArrayList;import java.util.List;import java.util.Stack;// Classic Non-Recursive algorithm for preorder traversal// 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 {public List<Integer> preorderTraversal(TreeNode root) {ArrayList<Integer> res = new ArrayList<Integer>();if(root == null)return res;Stack<TreeNode> stack = new Stack<TreeNode>();stack.push(root);while(!stack.empty()){TreeNode curNode = stack.pop();res.add(curNode.val);if(curNode.right != null)stack.push(curNode.right);if(curNode.left != null)stack.push(curNode.left);}return res;}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description//// Author : liuyubobobo/// Time : 2018-05-30import java.util.ArrayList;import java.util.List;import java.util.Stack;// Another Classic Non-Recursive algorithm for preorder traversal// Time Complexity: O(n), n is the node number in the tree// Space Complexity: O(h), h is the height of the treepublic class Solution4 {public List<Integer> preorderTraversal(TreeNode root) {ArrayList<Integer> res = new ArrayList<Integer>();if(root == null)return res;Stack<TreeNode> stack = new Stack<TreeNode>();TreeNode cur = root;while(cur != null || !stack.isEmpty()){while(cur != null){res.add(cur.val);stack.push(cur);cur = cur.left;}cur = stack.pop();cur = cur.right;}return res;}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description//// Author : liuyubobobo/// Time : 2018-05-30import java.util.ArrayList;import java.util.List;import java.util.Stack;// Another Classic Non-Recursive algorithm for preorder traversal// Time Complexity: O(n), n is the node number in the tree// Space Complexity: O(h), h is the height of the treepublic class Solution5 {public List<Integer> preorderTraversal(TreeNode root) {ArrayList<Integer> res = new ArrayList<Integer>();if(root == null)return res;Stack<TreeNode> stack = new Stack<TreeNode>();TreeNode cur = root;while(cur != null || !stack.isEmpty()){if(cur != null){res.add(cur.val);stack.push(cur);cur = cur.left;}else{cur = stack.pop();cur = cur.right;}}return res;}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description//// Author : liuyubobobo/// Time : 2018-05-29import java.util.ArrayList;import java.util.List;import java.util.Stack;// PreOrder Morris Traversal// Time Complexity: O(n), n is the node number in the tree// Space Complexity: O(1)public class Solution6 {public List<Integer> preorderTraversal(TreeNode root) {ArrayList<Integer> res = new ArrayList<Integer>();if(root == null)return res;TreeNode cur = root;while(cur != null){if(cur.left == null){res.add(cur.val);cur = cur.right;}else{TreeNode prev = cur.left;while(prev.right != null && prev.right != cur)prev = prev.right;if(prev.right == null){res.add(cur.val);prev.right = cur;cur = cur.left;}else{prev.right = null;cur = cur.right;}}}return res;}}