Reverse Level Order Traversal
Last Updated :
23 Jul, 2025
Given a binary tree, the task is to find its reverse level order traversal. The idea is to print the last level first, then the second last level, and so on. Like Level order traversal, every level is printed from left to right.
Examples:
Input:
Output:
4 5
2 3
1
[Naive Approach] Using Recursion - O(n^2) Time and O(h) Space:
The algorithm for reverse level order traversal involves printing nodes level by level, starting from the bottom-most level and moving up to the root. First, the height of the tree is determined, denoted as h. The traversal then begins from level h and proceeds upwards to level 1. In each iteration, printGivenLevel function is called to print nodes at the current level. It prints the node's data if the current level matches the required level. If not, the function recursively explores the left and right subtrees to find and print nodes at the required level.
C++
// A recursive C++ program to print
// reverse level order traversal
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node *left;
Node *right;
Node(int x) {
data = x;
left = right = nullptr;
}
};
// Function to find the height of
// the tree.
int height(Node *node) {
if (node == nullptr)
return 0;
int lheight = height(node->left);
int rheight = height(node->right);
return max(lheight, rheight) + 1;
}
// Print nodes at a given level
void printGivenLevel(Node *root, int nodeLevel, int reqLevel) {
if (root == nullptr)
return;
// if the required level is reached,
// then print the node.
if (nodeLevel == reqLevel)
cout << root->data << " ";
// else call function for left and
// right subtree.
else if (nodeLevel < reqLevel) {
printGivenLevel(root->left, nodeLevel + 1, reqLevel);
printGivenLevel(root->right, nodeLevel + 1, reqLevel);
}
}
// Function to print reverse
// level order traversal a tree
void reverseLevelOrder(Node *root) {
// find the height of the tree.
int h = height(root);
// Start printing from the lowest level.
for (int i = h; i >= 1; i--)
printGivenLevel(root, 1, i);
}
int main() {
// create hard coded 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);
reverseLevelOrder(root);
return 0;
}
C
// A recursive C program to print
// reverse level order traversal
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Function to find the height of the tree.
int height(struct Node* node) {
if (node == NULL)
return 0;
int lheight = height(node->left);
int rheight = height(node->right);
return (lheight > rheight ? lheight : rheight) + 1;
}
// Print nodes at a given level
void printGivenLevel(struct Node* root, int nodeLevel, int reqLevel) {
if (root == NULL)
return;
// if the required level is reached,
// then print the node.
if (nodeLevel == reqLevel)
printf("%d ", root->data);
// else call function for left and right subtree.
else if (nodeLevel < reqLevel) {
printGivenLevel(root->left, nodeLevel + 1, reqLevel);
printGivenLevel(root->right, nodeLevel + 1, reqLevel);
}
}
// Function to print REVERSE level order traversal of a tree
void reverseLevelOrder(struct Node* root) {
// find the height of the tree.
int h = height(root);
// Start printing from the lowest level.
for (int i = h; i >= 1; i--)
printGivenLevel(root, 1, i);
}
struct Node* createNode(int x) {
struct Node* node =
(struct Node*)malloc(sizeof(struct Node));
node->data = x;
node->left = NULL;
node->right = NULL;
return node;
}
int main() {
// create hard coded tree
// 1
// / \
// 2 3
// / \
// 4 5
struct Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
reverseLevelOrder(root);
return 0;
}
Java
// A recursive Java program to print
// reverse level order traversal
class Node {
int data;
Node left, right;
Node(int x) {
data = x;
left = right = null;
}
}
class GfG {
// Function to find the height of the tree.
static int height(Node node) {
if (node == null)
return 0;
int lheight = height(node.left);
int rheight = height(node.right);
return Math.max(lheight, rheight) + 1;
}
// Print nodes at a given level
static void printGivenLevel(Node root, int nodeLevel, int reqLevel) {
if (root == null)
return;
// if the required level is reached, print the node.
if (nodeLevel == reqLevel)
System.out.print(root.data + " ");
// else call function for left and right subtree.
else if (nodeLevel < reqLevel) {
printGivenLevel(root.left, nodeLevel + 1, reqLevel);
printGivenLevel(root.right, nodeLevel + 1, reqLevel);
}
}
// Function to print REVERSE level order traversal of a tree
static void reverseLevelOrder(Node root) {
// find the height of the tree.
int h = height(root);
// Start printing from the lowest level.
for (int i = h; i >= 1; i--)
printGivenLevel(root, 1, i);
}
public static void main(String[] args) {
// create hard coded 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);
reverseLevelOrder(root);
}
}
Python
# A recursive Python program to print
# reverse level order traversal
class Node:
def __init__(self, x):
self.data = x
self.left = None
self.right = None
# Function to find the height of the tree.
def height(node):
if node is None:
return 0
lheight = height(node.left)
rheight = height(node.right)
return max(lheight, rheight) + 1
# Print nodes at a given level
def print_given_level(root, node_level, req_level):
if root is None:
return
# if the required level is reached, print the node.
if node_level == req_level:
print(root.data, end=" ")
# else call function for left and right subtree.
elif node_level < req_level:
print_given_level(root.left, node_level + 1, req_level)
print_given_level(root.right, node_level + 1, req_level)
# Function to print REVERSE level order traversal of a tree
def reverse_level_order(root):
# find the height of the tree.
h = height(root)
# Start printing from the lowest level.
for i in range(h, 0, -1):
print_given_level(root, 1, i)
if __name__ == "__main__":
# create hard coded 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)
reverse_level_order(root)
C#
// A recursive C# program to print
// reverse level order traversal
using System;
class Node {
public int data;
public Node left, right;
public Node(int x) {
data = x;
left = right = null;
}
}
class GfG {
// Function to find the height of the tree.
static int height(Node node) {
if (node == null)
return 0;
int lheight = height(node.left);
int rheight = height(node.right);
return Math.Max(lheight, rheight) + 1;
}
// Print nodes at a given level
static void printGivenLevel(Node root, int nodeLevel, int reqLevel) {
if (root == null)
return;
// if the required level is reached, print the node.
if (nodeLevel == reqLevel)
Console.Write(root.data + " ");
// else call function for left and right subtree.
else if (nodeLevel < reqLevel) {
printGivenLevel(root.left, nodeLevel + 1, reqLevel);
printGivenLevel(root.right, nodeLevel + 1, reqLevel);
}
}
// Function to print REVERSE level order traversal of a tree
static void reverseLevelOrder(Node root) {
// find the height of the tree.
int h = height(root);
// Start printing from the lowest level.
for (int i = h; i >= 1; i--)
printGivenLevel(root, 1, i);
}
static void Main() {
// create hard coded 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);
reverseLevelOrder(root);
}
}
JavaScript
// A recursive Javascript program to print
// reverse level order traversal
class Node {
constructor(x) {
this.data = x;
this.left = null;
this.right = null;
}
}
// Function to find the height of the tree.
function height(node) {
if (node === null)
return 0;
let lheight = height(node.left);
let rheight = height(node.right);
return Math.max(lheight, rheight) + 1;
}
// Print nodes at a given level
function printGivenLevel(root, nodeLevel, reqLevel) {
if (root === null)
return;
// if the required level is reached, print the node.
if (nodeLevel === reqLevel)
console.log(root.data);
// else call function for left and right subtree.
else if (nodeLevel < reqLevel) {
printGivenLevel(root.left, nodeLevel + 1, reqLevel);
printGivenLevel(root.right, nodeLevel + 1, reqLevel);
}
}
// Function to print REVERSE level order traversal of a tree
function reverseLevelOrder(root) {
// find the height of the tree.
let h = height(root);
// Start printing from the lowest level.
for (let i = h; i >= 1; i--)
printGivenLevel(root, 1, i);
}
// create hard coded tree
// 1
// / \
// 2 3
// / \
// 4 5
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);
reverseLevelOrder(root);
Time Complexity: O(n^2)
Auxiliary Space: O(h), where h is the height of the tree.
[Expected Approach] Using Stack and Queue - O(n) Time and O(n) Space:
The approach for reverse level order traversal, using a stack and queue is conceptually similar to a standard level order traversal but with key modifications. Instead of printing each node as it is visited, the nodes are pushed onto a stack. Additionally, when enqueueing children nodes, the right child is enqueued before the left child. This ensures that when nodes are popped from the stack, they are printed in reverse level order.
Step by step implementation:
- The idea is to use a stack to get the reverse level order.
- Push the root node into a queue. While queue is not empty, perform steps 3,4.
- Pop the front element of the queue. Instead of printing it (like in level order), push node's value into a stack. This way the elements present on upper levels will be printed later than elements on lower levels.
- If the right child of current node exists, push it into queue before left child. This is because elements on the same level should be printed from left to right.
- While stack is not empty, pop the top element and print it.
C++
// C++ program to print reverse level
// order traversal using stack and queue
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* left;
Node* right;
Node (int x) {
data = x;
left = right = nullptr;
}
};
// Function to print REVERSE
// level order traversal of a tree
void reverseLevelOrder(Node* root) {
stack<Node*> st;
queue<Node*> q;
q.push(root);
while (!q.empty()) {
// Dequeue node
Node* curr = q.front();
q.pop();
st.push(curr);
// Enqueue right child
if (curr->right)
q.push(curr->right);
// Enqueue left child
if (curr->left)
q.push(curr->left);
}
// Pop all items from stack one by one and print them
while (!st.empty()) {
Node* curr = st.top();
cout << curr->data << " ";
st.pop();
}
}
int main() {
// Create hard coded 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);
reverseLevelOrder(root);
return 0;
}
Java
// Java program to print reverse level
// order traversal using stack and queue
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
class Node {
int data;
Node left, right;
Node(int x) {
data = x;
left = right = null;
}
}
class GfG {
// Function to print REVERSE level order traversal of a tree
static void reverseLevelOrder(Node root) {
Stack<Node> st = new Stack<>();
Queue<Node> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()) {
// Dequeue node
Node curr = q.poll();
st.push(curr);
// Enqueue right child
if (curr.right != null)
q.add(curr.right);
// Enqueue left child
if (curr.left != null)
q.add(curr.left);
}
// Pop all items from stack one by one and print them
while (!st.isEmpty()) {
Node curr = st.pop();
System.out.print(curr.data + " ");
}
}
public static void main(String[] args) {
// Create hard coded 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);
reverseLevelOrder(root);
}
}
Python
# Python program to print reverse level
# order traversal using stack and queue
from collections import deque
class Node:
def __init__(self, x):
self.data = x
self.left = None
self.right = None
# Function to print REVERSE level order traversal of a tree
def reverse_level_order(root):
st = []
q = deque([root])
while q:
# Dequeue node
curr = q.popleft()
st.append(curr)
# Enqueue right child
if curr.right:
q.append(curr.right)
# Enqueue left child
if curr.left:
q.append(curr.left)
# Pop all items from stack one by one and print them
while st:
curr = st.pop()
print(curr.data, end=" ")
if __name__ == "__main__":
# Create hard coded 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)
reverse_level_order(root)
C#
// C# program to print reverse level
// order traversal using stack and queue
using System;
using System.Collections.Generic;
class Node {
public int data;
public Node left, right;
public Node(int x) {
data = x;
left = right = null;
}
}
class GfG {
// Function to print REVERSE level order traversal of a tree
static void ReverseLevelOrder(Node root) {
Stack<Node> S = new Stack<Node>();
Queue<Node> Q = new Queue<Node>();
Q.Enqueue(root);
while (Q.Count > 0) {
// Dequeue node
Node curr = Q.Dequeue();
S.Push(curr);
// Enqueue right child
if (curr.right != null)
Q.Enqueue(curr.right);
// Enqueue left child
if (curr.left != null)
Q.Enqueue(curr.left);
}
// Pop all items from stack one by one and print them
while (S.Count > 0) {
Node curr = S.Pop();
Console.Write(curr.data + " ");
}
}
static void Main(string[] args) {
// Create a hard coded 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);
ReverseLevelOrder(root);
}
}
JavaScript
// Javascript program to print reverse level
// order traversal using stack and queue
class Node {
constructor(x) {
this.data = x;
this.left = null;
this.right = null;
}
}
// Function to print REVERSE level order traversal of a tree
function reverseLevelOrder(root) {
let S = [];
let Q = [];
Q.push(root);
while (Q.length > 0) {
// Dequeue node
let curr = Q.shift();
S.push(curr);
// Enqueue right child
if (curr.right)
Q.push(curr.right);
// Enqueue left child
if (curr.left)
Q.push(curr.left);
}
// pop all items from stack one by one and print them
while (S.length > 0) {
let curr = S.pop();
console.log(curr.data);
}
}
// create hard coded tree
// 1
// / \
// 2 3
// / \
// 4 5
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);
reverseLevelOrder(root);
Time Complexity: O(n), where n is the number of nodes in the binary tree.
Auxiliary Space: O(n)
[Alternate Approach] Using a hash map - O(n) Time and O(n) Space:
The approach to reverse level order traversal using a hash map involves storing nodes by their levels and then retrieving them in reverse order. To achieve this, a hash map is used where each level of the binary tree maps to a list of nodes at that level. During a pre-order traversal of the tree, nodes are added to the hash map based on their level, ensuring that nodes are processed from the leftmost node at each level first. If the current node is null, the function returns immediately. Otherwise, it adds the node to the list corresponding to its level in the hash map, then recursively processes the left and right children.
After completing the traversal, the height of the tree, denoted by the size of the hash map, determines the number of levels. The final step is to iterate over the hash map from the highest level down to level 1, printing nodes from each level to achieve the reverse level order traversal.
Below is the implementation of the above approach:
C++
//C++ program to print reverse level
// order traversal using hashmap
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* left;
Node* right;
Node (int x) {
data = x;
left = right = nullptr;
}
};
// Recursive function to traverse the binary
// tree and add nodes to the hashmap
void addNodesToMap(Node* node, int level,
unordered_map<int, vector<int> >& map) {
if (node == nullptr)
return;
// Add the current node to the vector of
// nodes at its level in the hashmap
map[level].push_back(node->data);
// Recursively traverse the left and
// right subtrees
addNodesToMap(node->left, level + 1, map);
addNodesToMap(node->right, level + 1, map);
}
vector<int> reverseLevelOrder(Node* root) {
vector<int> result;
// Create an unordered_map to store the
// nodes at each level of the binary tree
unordered_map<int, vector<int> > map;
// Traverse the binary tree recursively and
// add nodes to the hashmap
addNodesToMap(root, 0, map);
// Iterate over the hashmap in reverse order of the
// levels and add nodes to the result vector
for (int level = map.size() - 1; level >= 0;
level--) {
vector<int> nodesAtLevel = map[level];
for (int i = 0; i < nodesAtLevel.size(); i++) {
result.push_back(nodesAtLevel[i]);
}
}
return result;
}
void printList(vector<int> v) {
for (int i=0; i<v.size(); i++) {
cout << v[i] << " ";
}
cout<<endl;
}
int main() {
// create hard coded 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);
vector<int> result = reverseLevelOrder(root);
printList(result);
return 0;
}
Java
// Java program to print reverse level
// order traversal using hashmap
import java.util.*;
class Node {
int data;
Node left, right;
Node(int x) {
data = x;
left = right = null;
}
}
class GfG {
// Recursive function to traverse the binary
// tree and add nodes to the hashmap
static void addNodesToMap(Node node, int level, Map<Integer, List<Integer>> map) {
if (node == null)
return;
map.putIfAbsent(level, new ArrayList<>());
map.get(level).add(node.data);
// Recursively traverse the left and right subtrees
addNodesToMap(node.left, level + 1, map);
addNodesToMap(node.right, level + 1, map);
}
static List<Integer> reverseLevelOrder(Node root) {
ArrayList<Integer> result = new ArrayList<>();
// Create a HashMap to store the nodes
// at each level of the binary tree
Map<Integer, List<Integer>> map =
new HashMap<>();
// Traverse the binary tree recursively
// and add nodes to the hashmap
addNodesToMap(root, 0, map);
// Iterate over the hashmap in reverse
//order of the levels and add nodes
// to the result
for (int level = map.size() - 1;
level >= 0; level--) {
result.addAll(map.get(level));
}
return result;
}
public static void main(String[] args) {
// create hard coded 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);
List<Integer> result = reverseLevelOrder(root);
result.forEach(node -> System.out.print(node + " "));
}
}
Python
# Python program to print REVERSE level
# order traversal using hashmap
from collections import defaultdict
class Node:
def __init__(self, x):
self.data = x
self.left = None
self.right = None
# Recursive function to traverse the binary tree
# and add nodes to the hashmap
def add_nodes_to_map(node, level, map):
if node is None:
return
map[level].append(node.data)
# Recursively traverse the left and right subtrees
add_nodes_to_map(node.left, level + 1, map)
add_nodes_to_map(node.right, level + 1, map)
def reverse_level_order(root):
result = []
# Create a defaultdict to store the nodes
# at each level of the binary tree
map = defaultdict(list)
# Traverse the binary tree recursively
# and add nodes to the hashmap
add_nodes_to_map(root, 0, map)
# Iterate over the hashmap in reverse order of
# the levels and add nodes to the result
for level in reversed(range(len(map))):
result.extend(map[level])
return result
def print_list(lst):
print(" ".join(map(str, lst)))
if __name__ == "__main__":
# create hard coded 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)
result = reverse_level_order(root)
print_list(result)
C#
// A C# program to print REVERSE level
// order traversal using hashmap
using System;
using System.Collections.Generic;
class Node {
public int data;
public Node left, right;
public Node(int x) {
data = x;
left = right = null;
}
}
class GfG {
// Recursive function to traverse the binary tree
// and add nodes to the hashmap
static void AddNodesToMap(Node node, int level, Dictionary<int, List<int>> map) {
if (node == null)
return;
if (!map.ContainsKey(level))
map[level] = new List<int>();
map[level].Add(node.data);
// Recursively traverse the left and right subtrees
AddNodesToMap(node.left, level + 1, map);
AddNodesToMap(node.right, level + 1, map);
}
static List<int> ReverseLevelOrder(Node root) {
List<int> result = new List<int>();
// Create a Dictionary to store the
// nodes at each level of the binary tree
Dictionary<int, List<int>> map = new Dictionary<int, List<int>>();
// Traverse the binary tree recursively
// and add nodes to the hashmap
AddNodesToMap(root, 0, map);
// Iterate over the hashmap in reverse order
// of the levels and add nodes to the result
for (int level = map.Count - 1; level >= 0; level--) {
result.AddRange(map[level]);
}
return result;
}
static void PrintList(List<int> list) {
foreach (var item in list) {
Console.Write(item + " ");
}
Console.WriteLine();
}
static void Main(string[] args) {
// create hard coded 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);
List<int> result = ReverseLevelOrder(root);
PrintList(result);
}
}
JavaScript
// JavaScript program to print REVERSE level
// order traversal using hashmap
class Node {
constructor(x) {
this.data = x;
this.left = null;
this.right = null;
}
}
// Recursive function to traverse the binary tree
// and add nodes to the hashmap
function addNodesToMap(node, level, map) {
if (node === null) return;
if (!map.has(level)) map.set(level, []);
map.get(level).push(node.data);
// Recursively traverse the left and right subtrees
addNodesToMap(node.left, level + 1, map);
addNodesToMap(node.right, level + 1, map);
}
function reverseLevelOrder(root) {
const result = [];
const map = new Map();
// Traverse the binary tree recursively
// and add nodes to the hashmap
addNodesToMap(root, 0, map);
// Iterate over the hashmap in reverse order
// of the levels and add nodes to the result
for (let level = map.size - 1; level >= 0; level--) {
result.push(...map.get(level));
}
return result;
}
function printList(list) {
console.log(list.join(" "));
}
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);
const result = reverseLevelOrder(root);
printList(result);
Time complexity: O(n), where n is the number of nodes in the binary tree.
Auxiliary Space: O(n)
Reverse Level Order Traversal
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem