Preorder Traversal of Binary Tree
Last Updated :
28 Mar, 2025
Improve
Try it on GfG Practice
Preorder traversal is a tree traversal method that follows the Root-Left-Right order:
- The root node of the subtree is visited first.
- Next, the left subtree is recursively traversed.
- Finally, the right subtree is recursively traversed.
How does Preorder Traversal work?
Key Properties:
- Used in expression trees to generate prefix notation.
- Traverse the root node first.
Examples:
Input:
Output: 1 2 3
Explanation: The Preorder Traversal visits the nodes in the following order: Root, Left, Right. Therefore, we visit the root node 1, then the left node 2 and lastly the right node 3.Input :
Output: 1 2 4 5 3 6
Explanation: Preorder Traversal (Root → Left → Right). Visit 1 → 2 → 4 → 5 → 3 → 6, resulting in 1 2 4 5 3 6.
Algorithm :
- If the root is NULL, return;
- Process the root node (e.g., print its value);
- Recursively traverse the left subtree;
- Recursively traverse the right subtree.
#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 << " ";
// Recursion on left subtree
printPreorder(node->left);
// Recursion on right subtree
printPreorder(node->right);
}
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);
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);
}
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);
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);
}
}
class GFG {
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
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);
BinaryTree.printPreorder(tree.root);
}
}
# 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)
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)
printPreorder(root)
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;
}
}
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);
}
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);
printPreorder(root);
}
}
// 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);
}
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);
printPreorder(root);
}
main();
Output
1 2 4 5 3 6
Time Complexity: O(n)
Auxiliary Space: O(h), 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 log n (when the tree is a complete tree)
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