codeintuition-logo

Understanding the sorted traversal pattern


Some problems require us to traverse the nodes of a binary search tree in the sorted order of their values. Different traversal algorithms like preorder, postorder, etc., traverse the nodes of the tree following different sequences. However, the inorder traversal that follows the left-node-right sequence traverses the nodes in the tree in the sorted order(ascending) of their values. This is because a binary search tree follows the binary search property where all the nodes in the left subtree of a node have values smaller than it, and all the nodes in the right subtree have values greater than it. And so the inorder traversal can be used to traverse the nodes in a binary search tree in the sorted order(ascending) of its values.

The sorted traversal pattern is a classification of problems that can be solved using the sorted traversal technique.
Loading Image

The inorder traversal traverses the binary search tree in the sorted order (ascending) of node values.

Liking the course? Check our discounted plans to continue learning.