DEV Community

Jaspreet singh
Jaspreet singh

Posted on

Binary Tree InOrder Traversal

Problem Statement

Given the root of a binary tree, return its inorder traversal.

Inorder Traversal follows:

Left

↓

Root

↓

Right
Enter fullscreen mode Exit fullscreen mode

Brute Force Intuition

In an interview, you can explain it like this:

Visit the left subtree first, then the current node, and finally the right subtree. This naturally follows recursion.

Recursion automatically remembers the path back to parent nodes.

Complexity

  • Time Complexity: O(N)
  • Space Complexity: O(H)

Where:

  • N = Number of Nodes
  • H = Height of Tree

Recursive Code

class Solution {

    public List<Integer> inorderTraversal(TreeNode root) {

        List<Integer> ans = new ArrayList<>();

        inorder(root, ans);

        return ans;
    }

    private void inorder(TreeNode root,
                         List<Integer> ans) {

        if (root == null)
            return;

        inorder(root.left, ans);

        ans.add(root.val);

        inorder(root.right, ans);
    }
}
Enter fullscreen mode Exit fullscreen mode

Moving Towards the Optimal Iterative Approach

Recursion uses the call stack.

Can we simulate it ourselves?

Yes.

Use an explicit:

Stack
Enter fullscreen mode Exit fullscreen mode

Go left as much as possible.

Process node.

Move to right.


Pattern Recognition

Whenever you see:

  • Tree Traversal
  • Simulate Recursion

Think:

Stack


Optimal Java Solution (Iterative)

class Solution {

    public List<Integer> inorderTraversal(TreeNode root) {

        List<Integer> ans = new ArrayList<>();

        Stack<TreeNode> st = new Stack<>();

        TreeNode curr = root;

        while (curr != null || !st.isEmpty()) {

            while (curr != null) {

                st.push(curr);

                curr = curr.left;
            }

            curr = st.pop();

            ans.add(curr.val);

            curr = curr.right;
        }

        return ans;
    }
}
Enter fullscreen mode Exit fullscreen mode

Dry Run

        1
         \
          2
         /
        3
Enter fullscreen mode Exit fullscreen mode

Traversal:

Left

↓

Root

↓

Right
Enter fullscreen mode Exit fullscreen mode

Answer:

1 3 2
Enter fullscreen mode Exit fullscreen mode

Why Stack Works?

The stack stores parent nodes while moving left.

Once the left subtree finishes,

we process the parent and continue with its right subtree.


Complexity Analysis

Metric Complexity
Time Complexity O(N)
Space Complexity O(H)

Interview One-Liner

Use a stack to simulate recursion by visiting all left nodes first, then processing the node, and finally exploring the right subtree.


Pattern Learned

Left

↓

Root

↓

Right
Enter fullscreen mode Exit fullscreen mode

Similar Problems

  • Inorder Traversal
  • BST Iterator
  • Morris Traversal
  • Recover BST

Memory Trick

Go Left

↓

Pop

↓

Visit

↓

Go Right
Enter fullscreen mode Exit fullscreen mode

Whenever you hear:

"Inorder Traversal"

your brain should immediately think:

Left → Root → Right

Top comments (0)