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 TreeIn-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
- If the current node is null, return (base case).
- Recursively traverse the left subtree.
- Visit (process) the current node.
- Recursively traverse the right subtree.
In-order Worked Example
Input: Tree: root(2), left(1), right(3)
- Go left to node 1: no children, visit 1
- Back to root, visit 2
- Go right to node 3: no children, visit 3
Output: 1, 2, 3
Press ▶ Run to watch it step by step.
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 traverseBehind 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
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
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.