Binary trees are fundamental data structures in computer science and understanding their traversal is crucial for various applications. Traversing a binary tree means visiting all the nodes in a specific order. There are several traversal methods, each with its unique applications and benefits. This article will explore the main types of binary tree traversal: in-order, pre-order, post-order, and level-order.
Types of Binary Tree Traversal
In in-order traversal, the left child is visited first, followed by the node itself, and then the right child. This can be visualized as Left - Root - Right.
C++
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* left;
Node* right;
Node(int item) {
data = item;
left = right = nullptr;
}
};
class GFG {
public:
static void inOrderTraversal(Node* root) {
if (root == nullptr) return;
// Traverse the left subtree
inOrderTraversal(root->left);
// Visit the root node
cout << root->data << " ";
// Traverse the right subtree
inOrderTraversal(root->right);
}
};
int main() {
Node* root = new Node(2);
root->left = new Node(1);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
cout << "In-Order Traversal: ";
GFG::inOrderTraversal(root);
cout << endl;
return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
void inOrderTraversal(struct Node* root) {
if (root == NULL) return;
// Traverse the left subtree
inOrderTraversal(root->left);
// Visit the root node
printf("%d ", root->data);
// Traverse the right subtree
inOrderTraversal(root->right);
}
struct Node* newNode(int data) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
int main() {
struct Node* root = newNode(2);
root->left = newNode(1);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printf("In-Order Traversal: ");
inOrderTraversal(root);
printf("\n");
return 0;
}
Java
class Node {
int data;
Node left, right;
Node(int item) {
data = item;
left = right = null;
}
}
class GFG {
public static void inOrderTraversal(Node root) {
if (root == null) return;
// Traverse the left subtree
inOrderTraversal(root.left);
// Visit the root node
System.out.print(root.data + " ");
// Traverse the right subtree
inOrderTraversal(root.right);
}
public static void main(String[] args) {
Node root = new Node(2);
root.left = new Node(1);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
System.out.print("In-Order Traversal: ");
inOrderTraversal(root);
System.out.println();
}
}
Python
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def in_order_traversal(root):
if root is None:
return
# Traverse the left subtree
in_order_traversal(root.left)
# Visit the root node
print(root.data, end=" ")
# Traverse the right subtree
in_order_traversal(root.right)
if __name__ == "__main__":
root = Node(2)
root.left = Node(1)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("In-Order Traversal: ", end="")
in_order_traversal(root)
print()
C#
using System;
class Node {
public int data;
public Node left, right;
public Node(int item) {
data = item;
left = right = null;
}
}
class GFG {
public static void InOrderTraversal(Node root) {
if (root == null) return;
// Traverse the left subtree
InOrderTraversal(root.left);
// Visit the root node
Console.Write(root.data + " ");
// Traverse the right subtree
InOrderTraversal(root.right);
}
static void Main(string[] args) {
Node root = new Node(2);
root.left = new Node(1);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
Console.Write("In-Order Traversal: ");
InOrderTraversal(root);
Console.WriteLine();
}
}
JavaScript
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
function inOrderTraversal(root) {
if (root === null) return;
// Traverse the left subtree
inOrderTraversal(root.left);
// Visit the root node
console.log(root.data + " ");
// Traverse the right subtree
inOrderTraversal(root.right);
}
// Driver Code
let root = new Node(2);
root.left = new Node(1);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
// Perform inorder traversal
console.log("In-Order Traversal:");
inOrderTraversal(root);
OutputIn-Order Traversal: 4 1 5 2 3
Time Complexity: O(N)
Auxiliary Space: If we don’t consider the size of the stack for function calls then O(1) otherwise O(h) where h is the height of the tree.
Below are some important concepts in In-order Traversal:
In pre-order traversal, the node is visited first, followed by its left child and then its right child. This can be visualized as Root - Left - Right.
C++
#include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node(int data) {
this->data = data;
this->left = nullptr;
this->right = nullptr;
}
};
void preOrderTraversal(Node* root) {
if (root == nullptr) return;
// Visit the root node
cout << root->data << " ";
// Traverse the left subtree
preOrderTraversal(root->left);
// Traverse the right subtree
preOrderTraversal(root->right);
}
int main() {
// Create the following binary tree
// 1
// / \
// 2 3
// / \
// 4 5
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);
cout << "Pre-Order Traversal: ";
preOrderTraversal(root);
cout << endl;
return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
void preOrderTraversal(struct Node* root) {
if (root == NULL) return;
// Visit the root node
printf("%d ", root->data);
// Traverse the left subtree
preOrderTraversal(root->left);
// Traverse the right subtree
preOrderTraversal(root->right);
}
struct Node* newNode(int data) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
int main() {
// Create the following binary tree
// 1
// / \
// 2 3
// / \
// 4 5
struct Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printf("Pre-Order Traversal: ");
preOrderTraversal(root);
printf("\n");
return 0;
}
Java
class Node {
int data;
Node left, right;
// Constructor
Node(int item) {
data = item;
left = right = null;
}
}
class GFG {
public static void preOrderTraversal(Node root) {
if (root == null) return;
// Visit the root node
System.out.print(root.data + " ");
// Traverse the left subtree
preOrderTraversal(root.left);
// Traverse the right subtree
preOrderTraversal(root.right);
}
public static void main(String[] args) {
// Create the following binary tree
// 1
// / \
// 2 3
// / \
// 4 5
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);
System.out.print("Pre-Order Traversal: ");
preOrderTraversal(root);
System.out.println();
}
}
Python
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def pre_order_traversal(root):
if root is None:
return
# Visit the root node
print(root.data, end=" ")
# Traverse the left subtree
pre_order_traversal(root.left)
# Traverse the right subtree
pre_order_traversal(root.right)
if __name__ == "__main__":
# Create the following binary tree
# 1
# / \
# 2 3
# / \
# 4 5
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("Pre-Order Traversal: ", end="")
pre_order_traversal(root)
print()
C#
using System;
class Node {
public int data;
public Node left, right;
public Node(int item) {
data = item;
left = right = null;
}
}
class GFG {
public static void PreOrderTraversal(Node root) {
if (root == null) return;
// Visit the root node
Console.Write(root.data + " ");
// Traverse the left subtree
PreOrderTraversal(root.left);
// Traverse the right subtree
PreOrderTraversal(root.right);
}
static void Main(string[] args) {
// Create the following binary tree
// 1
// / \
// 2 3
// / \
// 4 5
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);
Console.Write("Pre-Order Traversal: ");
PreOrderTraversal(root);
Console.WriteLine();
}
}
JavaScript
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
function preOrderTraversal(root) {
if (root === null) return;
// Visit the root node
console.log(root.data + " ");
// Traverse the left subtree
preOrderTraversal(root.left);
// Traverse the right subtree
preOrderTraversal(root.right);
}
// Driver Code
let 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);
console.log("Pre-Order Traversal:");
preOrderTraversal(root);
OutputPre-Order Traversal: 1 2 4 5 3
Time Complexity: O(N)
Auxiliary Space: If we don’t consider the size of the stack for function calls then O(1) otherwise O(h) where h is the height of the tree.
Below are some important concepts in Pre-Order Traversal:
In post-order traversal, the left child is visited first, then the right child, and finally the node itself. This can be visualized as Left - Right - Root.
C++
#include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node(int data) {
this->data = data;
this->left = nullptr;
this->right = nullptr;
}
};
void postOrderTraversal(Node* root) {
if (root == nullptr) return;
// Traverse the left subtree
postOrderTraversal(root->left);
// Traverse the right subtree
postOrderTraversal(root->right);
// Visit the root node
cout << root->data << " ";
}
int main() {
// Create the following binary tree
// 1
// / \
// 2 3
// / \
// 4 5
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);
cout << "Post-Order Traversal: ";
postOrderTraversal(root);
cout << endl;
return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
void postOrderTraversal(struct Node* root) {
if (root == NULL) return;
// Traverse the left subtree
postOrderTraversal(root->left);
// Traverse the right subtree
postOrderTraversal(root->right);
// Visit the root node
printf("%d ", root->data);
}
struct Node* newNode(int data) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
int main() {
// Create the following binary tree
// 1
// / \
// 2 3
// / \
// 4 5
struct Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printf("Post-Order Traversal: ");
postOrderTraversal(root);
printf("\n");
return 0;
}
Java
class Node {
int data;
Node left, right;
// Constructor
Node(int item) {
data = item;
left = right = null;
}
}
class GFG {
public static void postOrderTraversal(Node root) {
if (root == null) return;
// Traverse the left subtree
postOrderTraversal(root.left);
// Traverse the right subtree
postOrderTraversal(root.right);
// Visit the root node
System.out.print(root.data + " ");
}
public static void main(String[] args) {
// Create the following binary tree
// 1
// / \
// 2 3
// / \
// 4 5
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);
System.out.print("Post-Order Traversal: ");
postOrderTraversal(root);
System.out.println();
}
}
Python
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def post_order_traversal(root):
if root is None:
return
# Traverse the left subtree
post_order_traversal(root.left)
# Traverse the right subtree
post_order_traversal(root.right)
# Visit the root node
print(root.data, end=" ")
if __name__ == "__main__":
# Create the following binary tree
# 1
# / \
# 2 3
# / \
# 4 5
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("Post-Order Traversal: ", end="")
post_order_traversal(root)
print()
C#
using System;
class Node {
public int data;
public Node left, right;
public Node(int item) {
data = item;
left = right = null;
}
}
class GFG {
public static void PostOrderTraversal(Node root) {
if (root == null) return;
// Traverse the left subtree
PostOrderTraversal(root.left);
// Traverse the right subtree
PostOrderTraversal(root.right);
// Visit the root node
Console.Write(root.data + " ");
}
static void Main(string[] args) {
// Create the following binary tree
// 1
// / \
// 2 3
// / \
// 4 5
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);
Console.Write("Post-Order Traversal: ");
PostOrderTraversal(root);
Console.WriteLine();
}
}
JavaScript
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
function postOrderTraversal(root) {
if (root === null) return;
// Traverse the left subtree
postOrderTraversal(root.left);
// Traverse the right subtree
postOrderTraversal(root.right);
// Visit the root node
console.log(root.data + " ");
}
// Driver Code
let 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);
console.log("Post-Order Traversal:");
postOrderTraversal(root);
OutputPost-Order Traversal: 4 5 2 3 1
Below are some important concepts in Post-Order Traversal:
In level-order traversal, the nodes are visited level by level, starting from the root node and then moving to the next level. This can be visualized as Level 1 - Level 2 - Level 3 - ....
C++
#include <iostream>
#include <queue>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node(int data) {
this->data = data;
this->left = nullptr;
this->right = nullptr;
}
};
void levelOrderTraversal(Node* root) {
if (root == nullptr) return;
queue<Node*> q;
q.push(root);
while (!q.empty()) {
Node* current = q.front();
q.pop();
// Visit the root node
cout << current->data << " ";
// Enqueue left child
if (current->left != nullptr) q.push(current->left);
// Enqueue right child
if (current->right != nullptr) q.push(current->right);
}
}
int main() {
// Create the following binary tree
// 1
// / \
// 2 3
// / \
// 4 5
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);
cout << "Level-Order Traversal: ";
levelOrderTraversal(root);
cout << endl;
return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct QueueNode {
struct Node* treeNode;
struct QueueNode* next;
};
void enqueue(struct QueueNode** front, struct QueueNode** rear, struct Node* treeNode) {
struct QueueNode* newNode = (struct QueueNode*)malloc(sizeof(struct QueueNode));
newNode->treeNode = treeNode;
newNode->next = NULL;
if (*rear == NULL) {
*front = *rear = newNode;
return;
}
(*rear)->next = newNode;
*rear = newNode;
}
struct Node* dequeue(struct QueueNode** front, struct QueueNode** rear) {
if (*front == NULL) return NULL;
struct Node* treeNode = (*front)->treeNode;
struct QueueNode* temp = *front;
*front = (*front)->next;
if (*front == NULL) *rear = NULL;
free(temp);
return treeNode;
}
int isEmpty(struct QueueNode* front) {
return front == NULL;
}
void levelOrderTraversal(struct Node* root) {
if (root == NULL) return;
struct QueueNode* front = NULL;
struct QueueNode* rear = NULL;
enqueue(&front, &rear, root);
while (!isEmpty(front)) {
struct Node* current = dequeue(&front, &rear);
// Visit the root node
printf("%d ", current->data);
// Enqueue left child
if (current->left != NULL) enqueue(&front, &rear, current->left);
// Enqueue right child
if (current->right != NULL) enqueue(&front, &rear, current->right);
}
}
struct Node* newNode(int data) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
int main() {
// Create the following binary tree
// 1
// / \
// 2 3
// / \
// 4 5
struct Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printf("Level-Order Traversal: ");
levelOrderTraversal(root);
printf("\n");
return 0;
}
Java
import java.util.LinkedList;
import java.util.Queue;
class Node {
int data;
Node left, right;
// Constructor
Node(int item) {
data = item;
left = right = null;
}
}
class GFG {
public static void levelOrderTraversal(Node root) {
if (root == null) return;
Queue<Node> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()) {
Node current = q.poll();
// Visit the root node
System.out.print(current.data + " ");
// Enqueue left child
if (current.left != null) q.add(current.left);
// Enqueue right child
if (current.right != null) q.add(current.right);
}
}
public static void main(String[] args) {
// Create the following binary tree
// 1
// / \
// 2 3
// / \
// 4 5
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);
System.out.print("Level-Order Traversal: ");
levelOrderTraversal(root);
System.out.println();
}
}
Python
from collections import deque
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def level_order_traversal(root):
if root is None:
return
q = deque([root])
while q:
current = q.popleft()
# Visit the root node
print(current.data, end=" ")
# Enqueue left child
if current.left:
q.append(current.left)
# Enqueue right child
if current.right:
q.append(current.right)
if __name__ == "__main__":
# Create the following binary tree
# 1
# / \
# 2 3
# / \
# 4 5
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("Level-Order Traversal: ", end="")
level_order_traversal(root)
print()
C#
using System;
using System.Collections.Generic;
class Node {
public int data;
public Node left, right;
public Node(int item) {
data = item;
left = right = null;
}
}
class GFG {
public static void LevelOrderTraversal(Node root) {
if (root == null) return;
Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
while (q.Count > 0) {
Node current = q.Dequeue();
// Visit the root node
Console.Write(current.data + " ");
// Enqueue left child
if (current.left != null) q.Enqueue(current.left);
// Enqueue right child
if (current.right != null) q.Enqueue(current.right);
}
}
static void Main(string[] args) {
// Create the following binary tree
// 1
// / \
// 2 3
// / \
// 4 5
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);
Console.Write("Level-Order Traversal: ");
LevelOrderTraversal(root);
Console.WriteLine();
}
}
JavaScript
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
function levelOrderTraversal(root) {
if (root === null) return;
let queue = [root];
while (queue.length > 0) {
let current = queue.shift();
// Visit the root node
console.log(current.data + " ");
// Enqueue left child
if (current.left !== null) queue.push(current.left);
// Enqueue right child
if (current.right !== null) queue.push(current.right);
}
}
// Driver Code
let 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);
console.log("Level-Order Traversal:");
levelOrderTraversal(root);
OutputLevel-Order Traversal: 1 2 3 4 5
Below are some important concepts in Level-Order Traversal:
Special Binary Tree Traversals
Quick Links :
Similar Reads
Binary Tree Traversal
Binary trees are fundamental data structures in computer science and understanding their traversal is crucial for various applications. Traversing a binary tree means visiting all the nodes in a specific order. There are several traversal methods, each with its unique applications and benefits. This
15+ min read
Diagonal Traversal of Binary Tree
Given a Binary Tree, the task is to print the diagonal traversal of the binary tree.Note: If the diagonal element are present in two different subtrees, then left subtree diagonal element should be taken first and then right subtree. Example: Input: Output: 8 10 14 3 6 7 13 1 4Explanation: The above
7 min read
Vertical Traversal of a Binary Tree
Given a Binary Tree, the task is to find its vertical traversal starting from the leftmost level to the rightmost level. If multiple nodes pass through a vertical line, they should be printed as they appear in the level order traversal of the tree. Examples: Input: Output: [[4], [2], [1, 5, 6], [3,
10 min read
DFS traversal of a Tree
Given a Binary tree, Traverse it using DFS using recursion. Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways. Generally, there are 2 widely used ways for traversing trees: DFS or Depth-F
15+ min read
Binary Tree Representation
Binary tree is a tree data structure (non-linear) in which each node can have at most two children which are referred to as the left child and the right child. The topmost node in a binary tree is called the root, and the bottom-most nodes are called leaves. A binary tree can be visualized as a hier
4 min read
Tree Traversal Techniques
Tree Traversal techniques include various ways to visit all the nodes of the tree. Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways. In this article, we will discuss all the tree travers
15+ min read
Triple Order Traversal of a Binary Tree
Given a Binary Tree, the task is to find its Triple Order Traversal. Triple Order Traversal is a tree traversal technique in which every node is traversed thrice in the following order: Visit the root nodeTraverse the left subtreeVisit the root nodeTraverse the right subtreeVisit the root node.Examp
7 min read
Double Order Traversal of a Binary Tree
Given a Binary Tree, the task is to find its Double Order Traversal. Double Order Traversal is a tree traversal technique in which every node is traversed twice in the following order: Visit the Node.Traverse the Left Subtree.Visit the Node.Traverse the Right Subtree.Examples: Input: Output: 1 7 4 4
6 min read
Inorder traversal of an N-ary Tree
Given an N-ary tree containing, the task is to print the inorder traversal of the tree. Examples:Â Input: N = 3Â Â Output: 5 6 2 7 3 1 4Input: N = 3Â Â Output: 2 3 5 1 4 6Â Approach: The inorder traversal of an N-ary tree is defined as visiting all the children except the last then the root and finall
6 min read
Reverse zigzag Traversal of a Binary Tree
Given a Binary Tree, the task is to print the Reverse Zig-zag order of the tree. In Zig-zag traversal starting from the first level go from left to right for odd-numbered levels and right to left for even-numbered levels. For Reverse Zig-zag traversal just print the reverse of this traversal.Example
10 min read