Given an N-ary tree and an array val[] which stores the values associated with all the nodes. Also given are a leaf node X and… Read More

# Tag Archives: DFS

Given a tree, and the weights of all the nodes and an integer x, the task is to find a node i such that weight[i]… Read More

Given a tree with N nodes which initially have no color and an array color[] of size N which represent the color of each node… Read More

Given a directed graph. The task is to check if the given graph is connected or not. Examples: Input: Output: Yes Input: Output: No Recommended:… Read More

Given an undirected graph g, the task is to print the number of connected components in the graph. Examples: Input: Output: 3 There are three connected… Read More

Given a tree with N nodes, and two integers K and V. The task is to find the Kth node in the DFS traversal of… Read More

Given a tree with n nodes and a number associated with every node. We can break any edge of the tree which will result in… Read More

Given a directed graph G with N vertices and M edges. The task is to find the length of the longest directed path in Graph.… Read More

Given an n-ary tree of n vertices and n-1 edges. The tree is given in the form of adjacency list. Find number of subtrees of… Read More

Given an n-ary tree rooted at vertex 1. The tree has n vertices and n-1 edges. Each node has a value associated with it and… Read More

Given a 2-D matrix mat[][] the task is count the number of connected components in the matrix. A connected component is formed by all equal… Read More

Given a connected graph with N vertices and M edges. The task is to print the lexicographically smallest DFS traversal of the graph starting from… Read More

Given a tree as set of edges such that every node has unique value. We are also given a value k, the task is to… Read More

Given a graph containing N nodes. For any permutation of nodes P1, P2, P3, …, PN the value of the permutation is defined as the… Read More

Given a directed graph and a node X. The task is to find the minimum number of edges that must be added to the graph… Read More