Depth-First Search (DFS)
Preorder Traversal (root-left-right):
Visit the root node before visiting any nodes inside the left or right subtrees. Here, the traversal is root – left child – right child. It means that the root node is traversed first then its left child and finally the right child.
void printPreorder(Node* node){
if (node == NULL){
return;
}
cout << node->data << " ";
printPreorder(node->left);
printPreorder(node->right);
}
Inorder Traversal (left-root-right):
Visit the root node after visiting all nodes inside the left subtree but before visiting any node within the right subtree. Here, the traversal is left child – root – right child. It means that the left child is traversed first then its root node and finally the right child.
void printInorder(Node* node){
if (node == NULL){
return;
}
printInorder(node->left);
cout << node->data << " ";
printInorder(node->right);
}
Postorder Traversal (left-right-root):
Visit the root node after visiting all the nodes of the left and right subtrees. Here, the traversal is left child – right child – root. It means that the left child has traversed first then the right child and finally its root node.
void printPostorder(Node* node){
if (node == NULL){
return;
}
printPostorder(node->left);
printPostorder(node->right);
cout << node->data << " ";
}
🥇 🥇 🥇
Other Important Questions List
Practice Questions List
💯 🔥 🚀