Binary Tree Inorder Traversal Solutions in Java
Number 94
Difficulty Medium
Acceptance 63.5%
Link LeetCode
Solutions
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal//// 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> inorderTraversal(TreeNode root) {ArrayList<Integer> res = new ArrayList<Integer>();inorderTraversal(root, res);return res;}private void inorderTraversal(TreeNode node, List<Integer> list){if(node != null){inorderTraversal(node.left, list);list.add(node.val);inorderTraversal(node.right, list);}}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal//// 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> inorderTraversal(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));stack.push(new Command("print", command.node));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-inorder-traversal//// Author : liuyubobobo/// Time : 2018-05-30import java.util.ArrayList;import java.util.List;import java.util.Stack;// Classic Non-Recursive algorithm for inorder 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> inorderTraversal(TreeNode root) {ArrayList<Integer> res = new ArrayList<Integer>();if(root == null)return res;Stack<TreeNode> stack = new Stack<>();TreeNode cur = root;while(cur != null || !stack.empty()){while(cur != null){stack.push(cur);cur = cur.left;}cur = stack.pop();res.add(cur.val);cur = cur.right;}return res;}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal//// Author : liuyubobobo/// Time : 2018-05-30import java.util.ArrayList;import java.util.List;import java.util.Stack;// Another Classic Non-Recursive algorithm for inorder 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> inorderTraversal(TreeNode root) {ArrayList<Integer> res = new ArrayList<Integer>();if(root == null)return res;Stack<TreeNode> stack = new Stack<>();TreeNode cur = root;while(cur != null || !stack.empty()){if(cur != null){stack.push(cur);cur = cur.left;}else{cur = stack.pop();res.add(cur.val);cur = cur.right;}}return res;}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal//// Author : liuyubobobo/// Time : 2018-05-30import java.util.ArrayList;import java.util.List;import java.util.Stack;// Inorder Morris Traversal// Time Complexity: O(n), n is the node number in the tree// Space Complexity: O(1)public class Solution5 {public List<Integer> inorderTraversal(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){prev.right = cur;cur = cur.left;}else{prev.right = null;res.add(cur.val);cur = cur.right;}}}return res;}}