Preorder Traversal of Binary Tree
Preorder traversal is defined as a type of tree traversal that follows the Root-Left-Right policy where:
- The root node of the subtree is visited first.
- Then the left subtree is traversed.
- At last, the right subtree is traversed.

Algorithm for Preorder Traversal of Binary Tree
The algorithm for preorder traversal is shown as follows:
Preorder(root):
- If root is NULL then return
- Process root (For example, print root’s data)
- Preorder (root -> left)
- Preorder (root -> right)
Examples of Preorder Traversal
Input:
Output: ABC
Explanation: The Preorder traversal visits the nodes in the following order: Root, Left, Right. Therefore, we visit the root node A, then visit the left node B and lastly the right node C.
Input:Output: ABDEC
Input: NULL
Output:
Output is empty in this case.
Program to Implement Preorder Traversal of Binary Tree
Below is the code implementation of the preorder traversal:
// C++ program for preorder traversals
#include <bits/stdc++.h>
using namespace std;
// Structure of a Binary Tree Node
struct Node {
int data;
struct Node *left, *right;
Node(int v)
{
data = v;
left = right = nullptr;
}
};
// Function to print preorder traversal
void printPreorder(struct Node* node)
{
if (node == nullptr)
return;
// Deal with the node
cout << node->data << " ";
// Recur on left subtree
printPreorder(node->left);
// Recur on right subtree
printPreorder(node->right);
}
// Driver code
int main()
{
struct Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
root->right->right = new Node(6);
// Function call
cout << "Preorder traversal of binary tree is: \n";
printPreorder(root);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Structure of a Binary Tree Node
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Function to create a new node
struct Node* newNode(int v) {
struct Node* node =
(struct Node*)malloc(sizeof(struct Node));
node->data = v;
node->left = NULL;
node->right = NULL;
return node;
}
// Function to print preorder traversal
void printPreorder(struct Node* node) {
if (node == NULL)
return;
// Deal with the node
printf("%d ", node->data);
// Recur on left subtree
printPreorder(node->left);
// Recur on right subtree
printPreorder(node->right);
}
// Driver code
int main() {
struct Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->right = newNode(6);
// Function call
printf("Preorder traversal of binary tree is:\n");
printPreorder(root);
printf("\n");
return 0;
}
import java.util.*;
// Structure of a Binary Tree Node
class Node {
int data;
Node left, right;
Node(int v) {
data = v;
left = right = null;
}
}
// Class for BinaryTree
class BinaryTree {
Node root;
// Function to print preorder traversal
public static void printPreorder(Node node) {
if (node == null)
return;
// First deal with the node
System.out.print(node.data + " ");
// Then recur on left subtree
printPreorder(node.left);
// Finally recur on right subtree
printPreorder(node.right);
}
}
// Main class
class GFG {
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
// Constructing the binary tree
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
tree.root.right.right = new Node(6);
// Function call
System.out.println("Preorder traversal of binary tree is: ");
BinaryTree.printPreorder(tree.root);
}
}
# Python program for preorder traversals
# Structure of a Binary Tree Node
class Node:
def __init__(self, v):
self.data = v
self.left = None
self.right = None
# Function to print preorder traversal
def printPreorder(node):
if node is None:
return
# Deal with the node
print(node.data, end=' ')
# Recur on left subtree
printPreorder(node.left)
# Recur on right subtree
printPreorder(node.right)
# Driver code
if __name__ == '__main__':
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.right = Node(6)
# Function call
print("Preorder traversal of binary tree is:")
printPreorder(root)
// C# program for preorder traversals
using System;
// Structure of a Binary Tree Node
public class Node {
public int data;
public Node left, right;
public Node(int v)
{
data = v;
left = right = null;
}
}
// Class to print preorder traversal
public class BinaryTree {
// Function to print preorder traversal
public static void printPreorder(Node node)
{
if (node == null)
return;
// Deal with the node
Console.Write(node.data + " ");
// Recur on left subtree
printPreorder(node.left);
// Recur on right subtree
printPreorder(node.right);
}
// Driver code
public static void Main()
{
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.right = new Node(6);
// Function call
Console.WriteLine(
"Preorder traversal of binary tree is: ");
printPreorder(root);
}
}
// This code is contributed by Susobhan Akhuli
// Structure of a Binary Tree Node
class Node {
constructor(v) {
this.data = v;
this.left = null;
this.right = null;
}
}
// Function to print preorder traversal
function printPreorder(node) {
if (node === null) {
return;
}
// Deal with the node
console.log(node.data);
// Recur on left subtree
printPreorder(node.left);
// Recur on right subtree
printPreorder(node.right);
}
// Driver code
function main() {
const root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.right = new Node(6);
// Function call
console.log("Preorder traversal of binary tree is:");
printPreorder(root);
}
main();
Output
Preorder traversal of binary tree is: 1 2 4 5 3 6
Complexity Analysis:
Time Complexity: O(N) where N is the total number of nodes. Because it traverses all the nodes at least once.
Auxiliary Space:
- O(1) if no recursion stack space is considered.
- Otherwise, O(h) where h is the height of the tree
- In the worst case, h can be the same as N (when the tree is a skewed tree)
- In the best case, h can be the same as logN (when the tree is a complete tree)
How does Preorder Traversal of Binary Tree work?
Consider the following tree:

Example of Binary Tree
If we perform a preorder traversal in this binary tree, then the traversal will be as follows:
Step 1: At first the root will be visited, i.e. node 1.
Node 1 is visited
Step 2: After this, traverse in the left subtree. Now the root of the left subtree is visited i.e., node 2 is visited.
Node 2 is visited
Step 3: Again the left subtree of node 2 is traversed and the root of that subtree i.e., node 4 is visited.
Node 4 is visited
Step 4: There is no subtree of 4 and the left subtree of node 2 is visited. So now the right subtree of node 2 will be traversed and the root of that subtree i.e., node 5 will be visited.
Node 5 is visited
Step 5: The left subtree of node 1 is visited. So now the right subtree of node 1 will be traversed and the root node i.e., node 3 is visited.
Node 3 is visited
Step 6: Node 3 has no left subtree. So the right subtree will be traversed and the root of the subtree i.e., node 6 will be visited. After that there is no node that is not yet traversed. So the traversal ends.
The complete tree is visited
So the order of traversal of nodes is 1 -> 2 -> 4 -> 5 -> 3 -> 6.
Use cases of Preorder Traversal:
Some use cases of preorder traversal are:
- It is also useful to get the prefix expression from an expression tree.
- Or any other case where we wish to traverse the root node first.
Related Articles:
- Types of Tree Traversal
- Iterative preorder traversal
- Check if given array can represent preorder traversal of BST
- Preorder from inorder and postorder traversals
- Find nth node in preorder traversal of a binary tree
- Preorder traversal of an N-ary tree