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
public List<Integer> inorderTraversal(TreeNode root) {
    ArrayList<Integer> result = new ArrayList<>();    
    Stack<TreeNode> stack = new Stack<>();
 
    TreeNode p = root;
    while(p!=null){
        stack.push(p);
        p=p.left;
    }
 
    while(!stack.isEmpty()){            
        TreeNode t = stack.pop();
        result.add(t.val);
 
        t = t.right;
        while(t!=null){
            stack.push(t);
            t = t.left;
        }
    }
 
    return result;
}

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

The following solution is simple, but it changes the tree structure, i.e., remove pointers to left and right children.

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;
}

15 thoughts on “Leetcode – Binary Tree Inorder Traversal (Java)”

  1. 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){
    lst.add(current.val);
    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;
    }
    }

  2. 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;

    }

  3. Your method type should be “Array-List”.
    and you never return or print your array-list as your result.

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

  5. 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;

    }

    }

    }

    }

  6. Alternatively, recursion would make this much easier:

    ArrayList treeContents = new ArrayList();

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

    inorderTraversal(node.right);
    }
    else {return null;}
    }

Leave a Comment