# Leetcode – Binary Tree Inorder Traversal (Java)

There are 3 solutions for solving this problem.

Java Solution 1 - Iterative

The key to solve inorder traversal of binary tree includes the following:

1. The order of "inorder" is: left child -> parent -> right child
2. Use a stack to track nodes
3. Understand when to push node into the stack and when to pop node out of the stack

```//Definition for binary tree public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } }   public class Solution { public ArrayList<Integer> inorderTraversal(TreeNode root) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. ArrayList<Integer> lst = new ArrayList<Integer>();   if(root == null) return lst;   Stack<TreeNode> stack = new Stack<TreeNode>(); //define a pointer to track nodes TreeNode p = root;   while(!stack.empty() || p != null){   // if it is not null, push to stack //and go down the tree to left if(p != null){ stack.push(p); p = p.left;   // if no left child // pop stack, process the node // then let p point to the right }else{ TreeNode t = stack.pop(); lst.add(t.val); p = t.right; } }   return lst; } }```

Java Solution 2 - Recursive

The recursive solution is trivial.

```public class Solution { List<Integer> result = new ArrayList<Integer>();   public List<Integer> inorderTraversal(TreeNode root) { if(root !=null){ helper(root); }   return result; }   public void helper(TreeNode p){ if(p.left!=null) helper(p.left);   result.add(p.val);   if(p.right!=null) helper(p.right); } }```

Java Solution 3 - Simple

Updated on 4/28/2016

```public List<Integer> inorderTraversal(TreeNode root) { List<Integer> result = new ArrayList<Integer>(); if(root==null) return result; Stack<TreeNode> stack = new Stack<TreeNode>(); stack.push(root);   while(!stack.isEmpty()){ TreeNode top = stack.peek(); if(top.left!=null){ stack.push(top.left); top.left=null; }else{ result.add(top.val); stack.pop(); if(top.right!=null){ stack.push(top.right); } } }   return result; }```

Java Solution 4

```public List<Integer> inorderTraversal(TreeNode root) { Stack<TreeNode> stack = new Stack<TreeNode>(); List<Integer> result = new ArrayList<Integer>();   TreeNode p = root; while(p!=null){ stack.push(p); p = p.left; }   while(!stack.isEmpty()){ TreeNode t = stack.pop(); result.add(t.val);   if(t.right!=null){   t= t.right; while(t!=null){ stack.push(t); t=t.left; } } }   return result; }```
Category >> Algorithms
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
```<pre><code>
String foo = "bar";
</code></pre>
```
• hming

wouldn’t top.left = null modify the original tree and hence a bad style?

• Programming Help

You could try for a full customized solution
http://www.helpwithprogramming.com/

• soham

This Solution uses an auxillary class MyTreeNode to wrap the OriginalTreeNode class in order to provide a processed tag on elements in stack. The code becomes more intuitive and required very little modification for other traversals.

import java.util.*;

//Definition for binary tree
class OriginalTreeNode {
int val;
OriginalTreeNode left;
OriginalTreeNode right;
OriginalTreeNode(int x) { val = x; }
}

//Definition for custom tree node which adds a tag
class MyTreeNode {
int val;
boolean processed;
OriginalTreeNode left;
OriginalTreeNode right;
MyTreeNode(OriginalTreeNode x)
{
val = x.val;
left = x.left;
right = x.right;
// initially set to false
processed = false;
}
}

class Solution {
public ArrayList inorderTraversal(OriginalTreeNode root) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
ArrayList lst = new ArrayList();

if(root == null)
return lst;

Stack stack = new Stack();
stack.push(new MyTreeNode(root));

while(!stack.empty()){

MyTreeNode current = stack.pop();

// if the left and right children are already added for it then just print it out.
if(current.processed){
continue;
}

// pushing right element as innermost on stack to visit it last
if(current.right != null)
stack.push(new MyTreeNode(current.right));

// then pushing the root and marking it as processed before doing so
current.processed = true;
stack.push(current);

// pushing left node as the outermost so it is printed first
if(current.left != null)
stack.push(new MyTreeNode(current.left));
}

return lst;
}
}

• skjdhlj

• Ojas Juneja

Another Iterative Approach:

``` public List inorderTraversal(TreeNode root) { List list = new ArrayList(); if (root == null) { return list; } Stack stack = new Stack(); stack.push(root); while (stack.size() > 0) { while (root.left != null) { root = root.left; stack.push(root); } list.add(stack.pop().val); while (root.right == null) { if (!stack.isEmpty()) { root = stack.pop(); list.add(root.val); } else { break; } } root = root.right; if (root == null) { break; } stack.push(root); } return list; } ```

• Hengameh

Your method type should be “Array-List”.

• Hengameh

Thank you. wish you have added Main method for running and testing, as well.

• charan

how to implement main method for this program..?

• Kirin

good algo, but treeContents may not work as expected as each time during recursion a new frame is created

• Piyush Dubey

Indeed it is. A friend just pointed it out. Apologies for the untrue comment. ðŸ™‚

• Hazard

Excellent !, and the Method is supposed to return TreeNode datatype value isn’t it?

• geekyprateek

public void i_inorder(treenode root)

{

Stack s=new Stack();

treenode t=root;

s.push(t);

while(!s.empty() || t!=null)

{

if(t!=null)

{

s.push(t.left);

t=t.left;

}

else

{

treenode temp=s.pop();

if(temp!=null)

{

System.out.println(temp.data);

s.push(temp.right);

t=temp.right;

}

}

}

}

• keyan

Alternatively, recursion would make this much easier:

ArrayList treeContents = new ArrayList();

public inorderTraversal(TreeNode node){
if (node != null){
inorderTraversal(node.left);