Data Structures · Visualization

Binary Tree: In-order

Binary Tree

In-order traversal visits the left subtree first, then the current node, then the right subtree (Left, Node, Right). Time: O(n). Space: O(h) for the recursion stack.

Big-O: Traversals visit every node exactly once: O(n).

Binary Tree: In-order

Binary Tree

In-order traversal visits the left subtree first, then the current node, then the right subtree (Left, Node, Right). For a binary search tree specifically, this produces values in sorted ascending order, which is why it is such a widely used traversal.

How In-order Works

  1. If the current node is null, return (base case).
  2. Recursively traverse the left subtree.
  3. Visit (process) the current node.
  4. Recursively traverse the right subtree.

In-order Worked Example

Input: Tree: root(2), left(1), right(3)

  1. Go left to node 1: no children, visit 1
  2. Back to root, visit 2
  3. Go right to node 3: no children, visit 3

Output: 1, 2, 3

Binary Tree: Introduction
A hierarchy of nodes where each node has at most two children — usually called "left" and "right".
45
20
10
30
40
65
50
60
70
80
Active Result
Binary Tree: Introduction
Press ▶ Run to watch it step by step.
Python · Binary Trees in Code
def inorder(n):
    if not n:
        return
    inorder(n.left)         # left
    print(n.val, end=" ")   # node
    inorder(n.right)        # right

# left -> root -> right
# (sorted order for a BST)
# example: in-order traverse
Input: Tree: root(2), left(1), right(3)

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

In-order Time & Space Complexity

TimeO(n)
SpaceO(h) for the recursion stack

When to Use In-order

  • Reading values out of a binary search tree in sorted order
  • Validating whether a tree satisfies the BST ordering property
  • Generating a sorted list from tree-based data

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.