Construct Ancestor Matrix from a Given Binary Tree
Last Updated :
23 Jul, 2025
Given a Binary Tree where all values are from 0 to n-1. Construct an ancestor matrix mat[n][n] where the ancestor matrix is defined as below.
- mat[i][j] = 1 if i is ancestor of j
- mat[i][j] = 0, otherwise
Examples:
Input:
Output: {{0 1 1}
{0 0 0}
{0 0 0}}
Input:
Output: {{0 0 0 0 0 0}
{1 0 0 0 1 0}
{0 0 0 1 0 0}
{0 0 0 0 0 0}
{0 0 0 0 0 0}
{1 1 1 1 1 0}}
[Expected Approach - 1] Using Pre-Order traversal and Ancestor Array - O(n^2) Time and O(n^2) Space
The idea is to traverse the tree. While traversing, keep track of ancestors in an array. When we visit a node, we add it to ancestor array and consider the corresponding row in the adjacency matrix. We mark all ancestors in its row as 1. Once a node and all its children are processed, we remove the node from ancestor array.
Below is the implementation of above approach:
C++
// C++ program Construct Ancestor Matrix
// from a Given Binary Tree
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* left, *right;
Node (int x) {
data = x;
left = nullptr;
right = nullptr;
}
};
// Count number of nodes in tree.
int nodeCount(Node* root) {
if (root == nullptr) return 0;
return nodeCount(root->left) +
nodeCount(root->right) + 1;
}
// Recursive function to fill ancestor matrix.
void ancestorMatrixRecur(Node* root, vector<vector<int>> &mat,
vector<int> &anc) {
if (root == nullptr) return;
int curr = root->data;
// Set ancestor values
for (int i=0; i<anc.size(); i++) {
mat[anc[i]][curr] = 1;
}
// Push the current node into anc.
anc.push_back(curr);
// Process left and right subtree.
ancestorMatrixRecur(root->left, mat, anc);
ancestorMatrixRecur(root->right, mat, anc);
// Pop the current node from anc.
anc.pop_back();
}
// Function to construct ancestor matrix.
vector<vector<int>> ancestorMatrix(Node* root) {
if (root == nullptr) return {{}};
int n = nodeCount(root);
vector<vector<int>> mat(n, vector<int>(n, 0));
// vector to store ancestor along all paths.
vector<int> anc;
ancestorMatrixRecur(root, mat, anc);
return mat;
}
int main() {
// Construct the following binary tree
// 5
// / \
// 1 2
// / \ /
// 0 4 3
Node *root = new Node(5);
root->left = new Node(1);
root->right = new Node(2);
root->left->left = new Node(0);
root->left->right = new Node(4);
root->right->left = new Node(3);
vector<vector<int>> mat = ancestorMatrix(root);
for (int i = 0; i < mat.size(); i++) {
for (int j = 0; j < mat[i].size(); j++) {
cout << mat[i][j] << " ";
}
cout << endl;
}
return 0;
}
Java
// Java program Construct Ancestor Matrix
// from a Given Binary Tree
import java.util.ArrayList;
import java.util.List;
class Node {
int data;
Node left, right;
Node (int x) {
data = x;
left = null;
right = null;
}
}
class GfG {
// Count number of nodes in tree.
static int nodeCount(Node root) {
if (root == null) return 0;
return nodeCount(root.left) +
nodeCount(root.right) + 1;
}
// Recursive function to fill ancestor matrix.
static void ancestorMatrixRecur(Node root, List<List<Integer>> mat,
List<Integer> anc) {
if (root == null) return;
int curr = root.data;
// Set ancestor values
for (int i = 0; i < anc.size(); i++) {
mat.get(anc.get(i)).set(curr, 1);
}
// Push the current node into anc.
anc.add(curr);
// Process left and right subtree.
ancestorMatrixRecur(root.left, mat, anc);
ancestorMatrixRecur(root.right, mat, anc);
// Pop the current node from anc.
anc.remove(anc.size() - 1);
}
// Function to construct ancestor matrix.
static List<List<Integer>> ancestorMatrix(Node root) {
if (root == null) return new ArrayList<>();
int n = nodeCount(root);
List<List<Integer>> mat = new ArrayList<>();
for (int i = 0; i < n; i++) {
List<Integer> row = new ArrayList<>();
for (int j = 0; j < n; j++) {
row.add(0);
}
mat.add(row);
}
// List to store ancestor along all paths.
List<Integer> anc = new ArrayList<>();
ancestorMatrixRecur(root, mat, anc);
return mat;
}
public static void main(String[] args) {
// Construct the following binary tree
// 5
// / \
// 1 2
// / \ /
// 0 4 3
Node root = new Node(5);
root.left = new Node(1);
root.right = new Node(2);
root.left.left = new Node(0);
root.left.right = new Node(4);
root.right.left = new Node(3);
List<List<Integer>> mat = ancestorMatrix(root);
for (List<Integer> row : mat) {
for (int val : row) {
System.out.print(val + " ");
}
System.out.println();
}
}
}
Python
# Python program Construct Ancestor Matrix
# from a Given Binary Tree
class Node:
def __init__(self, x):
self.data = x
self.left = None
self.right = None
# Count number of nodes in tree.
def nodeCount(root):
if root is None:
return 0
return nodeCount(root.left) + nodeCount(root.right) + 1
# Recursive function to fill ancestor matrix.
def ancestorMatrixRecur(root, mat, anc):
if root is None:
return
curr = root.data
# Set ancestor values
for i in range(len(anc)):
mat[anc[i]][curr] = 1
# Push the current node into anc.
anc.append(curr)
# Process left and right subtree.
ancestorMatrixRecur(root.left, mat, anc)
ancestorMatrixRecur(root.right, mat, anc)
# Pop the current node from anc.
anc.pop()
# Function to construct ancestor matrix.
def ancestorMatrix(root):
if root is None:
return []
n = nodeCount(root)
mat = [[0 for _ in range(n)] for _ in range(n)]
# List to store ancestor along all paths.
anc = []
ancestorMatrixRecur(root, mat, anc)
return mat
if __name__ == "__main__":
# Construct the following binary tree
# 5
# / \
# 1 2
# / \ /
# 0 4 3
root = Node(5)
root.left = Node(1)
root.right = Node(2)
root.left.left = Node(0)
root.left.right = Node(4)
root.right.left = Node(3)
mat = ancestorMatrix(root)
for row in mat:
print(" ".join(map(str, row)))
C#
// C# program Construct Ancestor Matrix
// from a Given Binary Tree
using System;
using System.Collections.Generic;
class Node {
public int data;
public Node left, right;
public Node(int x) {
data = x;
left = null;
right = null;
}
}
class GfG {
// Count number of nodes in tree.
static int nodeCount(Node root) {
if (root == null) return 0;
return nodeCount(root.left) +
nodeCount(root.right) + 1;
}
// Recursive function to fill ancestor matrix.
static void ancestorMatrixRecur(Node root, List<List<int>> mat,
List<int> anc) {
if (root == null) return;
int curr = root.data;
// Set ancestor values
for (int i = 0; i < anc.Count; i++) {
mat[anc[i]][curr] = 1;
}
// Push the current node into anc.
anc.Add(curr);
// Process left and right subtree.
ancestorMatrixRecur(root.left, mat, anc);
ancestorMatrixRecur(root.right, mat, anc);
// Pop the current node from anc.
anc.RemoveAt(anc.Count - 1);
}
// Function to construct ancestor matrix.
static List<List<int>> ancestorMatrix(Node root) {
if (root == null) return new List<List<int>>();
int n = nodeCount(root);
List<List<int>> mat = new List<List<int>>();
for (int i = 0; i < n; i++) {
List<int> row = new List<int>();
for (int j = 0; j < n; j++) {
row.Add(0);
}
mat.Add(row);
}
// List to store ancestor along all paths.
List<int> anc = new List<int>();
ancestorMatrixRecur(root, mat, anc);
return mat;
}
static void Main(string[] args) {
// Construct the following binary tree
// 5
// / \
// 1 2
// / \ /
// 0 4 3
Node root = new Node(5);
root.left = new Node(1);
root.right = new Node(2);
root.left.left = new Node(0);
root.left.right = new Node(4);
root.right.left = new Node(3);
List<List<int>> mat = ancestorMatrix(root);
foreach (List<int> row in mat) {
foreach (int val in row) {
Console.Write(val + " ");
}
Console.WriteLine();
}
}
}
JavaScript
// Javascript program Construct Ancestor Matrix
// from a Given Binary Tree
class Node {
constructor(x) {
this.data = x;
this.left = null;
this.right = null;
}
}
// Count number of nodes in tree.
function nodeCount(root) {
if (root === null) return 0;
return nodeCount(root.left) +
nodeCount(root.right) + 1;
}
// Recursive function to fill ancestor matrix.
function ancestorMatrixRecur(root, mat, anc) {
if (root === null) return;
const curr = root.data;
// Set ancestor values
for (let i = 0; i < anc.length; i++) {
mat[anc[i]][curr] = 1;
}
// Push the current node into anc.
anc.push(curr);
// Process left and right subtree.
ancestorMatrixRecur(root.left, mat, anc);
ancestorMatrixRecur(root.right, mat, anc);
// Pop the current node from anc.
anc.pop();
}
// Function to construct ancestor matrix.
function ancestorMatrix(root) {
if (root === null) return [];
const n = nodeCount(root);
const mat = Array.from({ length: n }, () => Array(n).fill(0));
// List to store ancestor along all paths.
const anc = [];
ancestorMatrixRecur(root, mat, anc);
return mat;
}
// Construct the following binary tree
// 5
// / \
// 1 2
// / \ /
// 0 4 3
const root = new Node(5);
root.left = new Node(1);
root.right = new Node(2);
root.left.left = new Node(0);
root.left.right = new Node(4);
root.right.left = new Node(3);
const mat = ancestorMatrix(root);
mat.forEach(row => {
console.log(row.join(" "));
});
Output0 0 0 0 0 0
1 0 0 0 1 0
0 0 0 1 0 0
0 0 0 0 0 0
0 0 0 0 0 0
1 1 1 1 1 0
[Expected Approach - 2] Using Post-Order Traversal - O(n^2) Time and O(h) Space
The idea is to perform recursive traversal of the binary tree. For each node, process the left and right nodes. Set current node as ancestor of all nodes for which left and right nodes are ancestors. Then set current node as ancestor of left and right node.
Below is the implementation of the above approach:
C++
// C++ program Construct Ancestor Matrix
// from a Given Binary Tree
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* left, *right;
Node (int x) {
data = x;
left = nullptr;
right = nullptr;
}
};
// Count number of nodes in tree.
int nodeCount(Node* root) {
if (root == nullptr) return 0;
return nodeCount(root->left) +
nodeCount(root->right) + 1;
}
// Recursive function to fill ancestor matrix.
void ancestorMatrixRecur(Node* root, vector<vector<int>> &mat) {
int curr = root->data;
if (root->left != nullptr) {
int left = root->left->data;
// Process the left subtree.
ancestorMatrixRecur(root->left, mat);
// Set curr node as ancestor of nodes
// for which left node is also ancestor.
for (int j = 0; j < mat.size(); j++) {
if (mat[left][j] == 1)
mat[curr][j] = 1;
}
// Set curr node as ancestor of left
// node.
mat[curr][left] = 1;
}
if (root->right != nullptr) {
int right = root->right->data;
// Process the right subtree.
ancestorMatrixRecur(root->right, mat);
// Set curr node as ancestor of nodes
// for which right node is also ancestor.
for (int j=0; j<mat.size(); j++) {
if (mat[right][j] == 1)
mat[curr][j] = 1;
}
// Set curr node as ancestor of right
// node.
mat[curr][right] = 1;
}
}
// Function to construct ancestor matrix.
vector<vector<int>> ancestorMatrix(Node* root) {
if (root == nullptr) return {{}};
int n = nodeCount(root);
vector<vector<int>> mat(n, vector<int>(n, 0));
ancestorMatrixRecur(root, mat);
return mat;
}
int main() {
// Construct the following binary tree
// 5
// / \
// 1 2
// / \ /
// 0 4 3
Node *root = new Node(5);
root->left = new Node(1);
root->right = new Node(2);
root->left->left = new Node(0);
root->left->right = new Node(4);
root->right->left = new Node(3);
vector<vector<int>> mat = ancestorMatrix(root);
for (int i=0; i< mat.size(); i++) {
for (int j=0; j<mat[i].size(); j++) {
cout << mat[i][j] << " ";
}
cout << endl;
}
return 0;
}
Java
// Java program Construct Ancestor Matrix
// from a Given Binary Tree
import java.util.ArrayList;
class Node {
int data;
Node left, right;
Node (int x) {
data = x;
left = null;
right = null;
}
}
class GfG {
// Count number of nodes in tree.
static int nodeCount(Node root) {
if (root == null) return 0;
return nodeCount(root.left) +
nodeCount(root.right) + 1;
}
// Recursive function to fill ancestor matrix.
static void ancestorMatrixRecur(Node root,
ArrayList<ArrayList<Integer>> mat) {
int curr = root.data;
if (root.left != null) {
int left = root.left.data;
// Process the left subtree.
ancestorMatrixRecur(root.left, mat);
// Set curr node as ancestor of nodes
// for which left node is also ancestor.
for (int j = 0; j < mat.size(); j++) {
if (mat.get(left).get(j) == 1)
mat.get(curr).set(j, 1);
}
// Set curr node as ancestor of left
// node.
mat.get(curr).set(left, 1);
}
if (root.right != null) {
int right = root.right.data;
// Process the right subtree.
ancestorMatrixRecur(root.right, mat);
// Set curr node as ancestor of nodes
// for which right node is also ancestor.
for (int j = 0; j < mat.size(); j++) {
if (mat.get(right).get(j) == 1)
mat.get(curr).set(j, 1);
}
// Set curr node as ancestor of right
// node.
mat.get(curr).set(right, 1);
}
}
// Function to construct ancestor matrix.
static ArrayList<ArrayList<Integer>> ancestorMatrix(Node root) {
if (root == null) return new ArrayList<>();
int n = nodeCount(root);
ArrayList<ArrayList<Integer>> mat = new ArrayList<>();
for (int i = 0; i < n; i++) {
ArrayList<Integer> row = new ArrayList<>();
for (int j = 0; j < n; j++) {
row.add(0);
}
mat.add(row);
}
ancestorMatrixRecur(root, mat);
return mat;
}
public static void main(String[] args) {
// Construct the following binary tree
// 5
// / \
// 1 2
// / \ /
// 0 4 3
Node root = new Node(5);
root.left = new Node(1);
root.right = new Node(2);
root.left.left = new Node(0);
root.left.right = new Node(4);
root.right.left = new Node(3);
ArrayList<ArrayList<Integer>> mat = ancestorMatrix(root);
for (ArrayList<Integer> row : mat) {
for (int val : row) {
System.out.print(val + " ");
}
System.out.println();
}
}
}
Python
# Python program Construct Ancestor Matrix
# from a Given Binary Tree
class Node:
def __init__(self, x):
self.data = x
self.left = None
self.right = None
# Count number of nodes in tree.
def nodeCount(root):
if root is None:
return 0
return nodeCount(root.left) + nodeCount(root.right) + 1
# Recursive function to fill ancestor matrix.
def ancestorMatrixRecur(root, mat):
curr = root.data
if root.left is not None:
left = root.left.data
# Process the left subtree.
ancestorMatrixRecur(root.left, mat)
# Set curr node as ancestor of nodes
# for which left node is also ancestor.
for j in range(len(mat)):
if mat[left][j] == 1:
mat[curr][j] = 1
# Set curr node as ancestor of left
# node.
mat[curr][left] = 1
if root.right is not None:
right = root.right.data
# Process the right subtree.
ancestorMatrixRecur(root.right, mat)
# Set curr node as ancestor of nodes
# for which right node is also ancestor.
for j in range(len(mat)):
if mat[right][j] == 1:
mat[curr][j] = 1
# Set curr node as ancestor of right
# node.
mat[curr][right] = 1
# Function to construct ancestor matrix.
def ancestorMatrix(root):
if root is None:
return [[0]]
n = nodeCount(root)
mat = [[0] * n for _ in range(n)]
ancestorMatrixRecur(root, mat)
return mat
if __name__ == "__main__":
# Construct the following binary tree
# 5
# / \
# 1 2
# / \ /
# 0 4 3
root = Node(5)
root.left = Node(1)
root.right = Node(2)
root.left.left = Node(0)
root.left.right = Node(4)
root.right.left = Node(3)
mat = ancestorMatrix(root)
for row in mat:
print(" ".join(map(str, row)))
C#
// C# program Construct Ancestor Matrix
// from a Given Binary Tree
using System;
using System.Collections.Generic;
class Node {
public int data;
public Node left, right;
public Node(int x) {
data = x;
left = null;
right = null;
}
}
class GfG {
// Count number of nodes in tree.
static int nodeCount(Node root) {
if (root == null) return 0;
return nodeCount(root.left) +
nodeCount(root.right) + 1;
}
// Recursive function to fill ancestor matrix.
static void ancestorMatrixRecur(Node root,
List<List<int>> mat) {
int curr = root.data;
if (root.left != null) {
int left = root.left.data;
// Process the left subtree.
ancestorMatrixRecur(root.left, mat);
// Set curr node as ancestor of nodes
// for which left node is also ancestor.
for (int j = 0; j < mat.Count; j++) {
if (mat[left][j] == 1)
mat[curr][j] = 1;
}
// Set curr node as ancestor of left
// node.
mat[curr][left] = 1;
}
if (root.right != null) {
int right = root.right.data;
// Process the right subtree.
ancestorMatrixRecur(root.right, mat);
// Set curr node as ancestor of nodes
// for which right node is also ancestor.
for (int j = 0; j < mat.Count; j++) {
if (mat[right][j] == 1)
mat[curr][j] = 1;
}
// Set curr node as ancestor of right
// node.
mat[curr][right] = 1;
}
}
// Function to construct ancestor matrix.
static List<List<int>> ancestorMatrix(Node root) {
if (root == null) return new List<List<int>>();
int n = nodeCount(root);
List<List<int>> mat = new List<List<int>>();
for (int i = 0; i < n; i++) {
List<int> row = new List<int>();
for (int j = 0; j < n; j++) {
row.Add(0);
}
mat.Add(row);
}
ancestorMatrixRecur(root, mat);
return mat;
}
static void Main(string[] args) {
// Construct the following binary tree
// 5
// / \
// 1 2
// / \ /
// 0 4 3
Node root = new Node(5);
root.left = new Node(1);
root.right = new Node(2);
root.left.left = new Node(0);
root.left.right = new Node(4);
root.right.left = new Node(3);
List<List<int>> mat = ancestorMatrix(root);
foreach (var row in mat) {
foreach (var val in row) {
Console.Write(val + " ");
}
Console.WriteLine();
}
}
}
JavaScript
// Javascript program Construct Ancestor Matrix
// from a Given Binary Tree
class Node {
constructor(x) {
this.data = x;
this.left = null;
this.right = null;
}
}
// Count number of nodes in tree.
function nodeCount(root) {
if (root === null) return 0;
return nodeCount(root.left) +
nodeCount(root.right) + 1;
}
// Recursive function to fill ancestor matrix.
function ancestorMatrixRecur(root, mat) {
const curr = root.data;
if (root.left !== null) {
const left = root.left.data;
// Process the left subtree.
ancestorMatrixRecur(root.left, mat);
// Set curr node as ancestor of nodes
// for which left node is also ancestor.
for (let j = 0; j < mat.length; j++) {
if (mat[left][j] === 1)
mat[curr][j] = 1;
}
// Set curr node as ancestor of left
// node.
mat[curr][left] = 1;
}
if (root.right !== null) {
const right = root.right.data;
// Process the right subtree.
ancestorMatrixRecur(root.right, mat);
// Set curr node as ancestor of nodes
// for which right node is also ancestor.
for (let j = 0; j < mat.length; j++) {
if (mat[right][j] === 1)
mat[curr][j] = 1;
}
// Set curr node as ancestor of right
// node.
mat[curr][right] = 1;
}
}
// Function to construct ancestor matrix.
function ancestorMatrix(root) {
if (root === null) return [[]];
const n = nodeCount(root);
const mat = Array.from({ length: n },
() => Array(n).fill(0));
ancestorMatrixRecur(root, mat);
return mat;
}
// Construct the following binary tree
// 5
// / \
// 1 2
// / \ /
// 0 4 3
const root = new Node(5);
root.left = new Node(1);
root.right = new Node(2);
root.left.left = new Node(0);
root.left.right = new Node(4);
root.right.left = new Node(3);
const mat = ancestorMatrix(root);
for (let i = 0; i < mat.length; i++) {
for (let j = 0; j < mat[i].length; j++) {
process.stdout.write(mat[i][j] + " ");
}
console.log();
}
Output0 0 0 0 0 0
1 0 0 0 1 0
0 0 0 1 0 0
0 0 0 0 0 0
0 0 0 0 0 0
1 1 1 1 1 0
Related articles:
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