AlgoLab
AlgorithmsCompare
AlgoLab
AlgorithmsCompareGitHub

© 2026 AlgoLab. Open source.

AlgorithmsTreesInorder Traversal
Treesbeginnertraversaltreerecursivesorted

Inorder Traversal

Inorder traversal visits nodes in the order: left subtree → current node → right subtree. For a BST, inorder traversal produces values in sorted ascending order. It is implemented simply with recursion or with an explicit stack for the iterative version.

Complexity

Best
O(n)
Average
O(n)
Worst
O(n)
Space
O(h)

Visualizer

Implementation

void inorder(Node* root, vector<int>& result) {
    if (!root) return;
    inorder(root->left, result);
    result.push_back(root->val);
    inorder(root->right, result);
}

How It Works

1.Left-Root-Right

Inorder traversal follows the pattern: recurse left, visit node, recurse right. For a BST this produces values in ascending sorted order, which is why it is the most commonly used tree traversal.

2.Applications

Inorder traversal is used to: print BST values sorted, validate a BST (check if inorder output is strictly increasing), and convert BST to sorted array.

3.Iterative Version

Use an explicit stack: push nodes going left until null, then pop, process, and go right. This avoids recursion stack overflow for very deep trees.

Related Algorithms

GitBranchPreorder & PostorderGitBranchLevel Order TraversalGitBranchBST Insert & Search

Test Your Knowledge

Quiz coming soon for this algorithm.

Back to all algorithms