Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. It starts at the root (selecting some arbitrary node as the root in the case of a graph) and explore as far as possible along each branch before backtracking.
The following graph shows the order in which the nodes are discovered in DFS:
In this post, we have listed out commonly asked interview questions that can be solved using DFS:
- Depth First Search (DFS)
- Arrival and departure time of vertices in DFS
- Types of edges involved in DFS and relation between them
- Determine whether a graph is Bipartite using DFS
- Topological Sort Algorithm for DAG
- Transitive closure of a graph
- Determine whether an undirected graph is a tree (Acyclic Connected Graph)
- 2–Edge Connectivity in a graph
- 2–Vertex Connectivity in a graph
- Check if a digraph is a DAG (Directed Acyclic Graph) or not
- Check if a graph is strongly connected or not
- Check if a graph is strongly connected or not using one DFS Traversal
- Find the cost of the shortest path in DAG using one pass of Bellman–Ford
- Find correct order of alphabets in a given dictionary of ancient origin
- Find the longest path in a Directed Acyclic Graph (DAG)
- Eulerian cycle in directed graphs
- Find root vertex of a graph
- Check whether an undirected graph is Eulerian
- Check if a set of words can be rearranged to form a circle
- Check if an undirected graph contains a cycle or not
- Traverse a given directory using BFS and DFS in Java
- Find the path between given vertices in a directed graph
- Construct a directed graph from an undirected graph that satisfies given constraints
- Replace all occurrences of 0 that are not surrounded by 1 in a binary matrix
- Replace all occurrences of 0 that are surrounded by 1 in a binary matrix
- Find the path from source to destination in a matrix that satisfies given constraints
- Find all occurrences of the given string in a character matrix
- Find the length of the longest path in a matrix with consecutive characters
- Flood Fill Algorithm
- Generate a list of possible words from a character matrix
- Lexicographic sorting of a given set of keys
- Find the maximum occurring word in a given set of strings
- Find first
kmaximum occurring words in a given set of strings - Find the shortest unique prefix for every word in an array
- Inorder Tree Traversal
- Preorder Tree Traversal
- Postorder Tree Traversal
- Print bottom view of a binary tree
- Print top view of a binary tree
- In-place convert a binary tree to its sum tree
- Determine whether the given binary tree nodes are cousins of each other
- Print cousins of a given node in a binary tree
- Check if a binary tree is a sum tree or not
- Determine whether a binary tree is a subtree of another binary tree
- Find the diameter of a binary tree
- Convert a binary tree to its mirror
- Print all paths from the root to leaf nodes of a binary tree
- Find ancestors of a given node in a binary tree
- Find the diagonal sum of a binary tree
- Sink nodes containing zero to the bottom of a binary tree
- Convert a binary tree to a full tree by removing half nodes
- Truncate a binary tree to remove nodes that lie on a path having a sum less than
k - Find maximum sum root to leaf path in a binary tree
- Check if a binary tree is height-balanced or not
- Convert binary tree to Left-child right-sibling binary tree
- Print all paths from leaf to root node of a binary tree
- Iteratively print the leaf to root path for every leaf node in a binary tree
- Find all nodes at a given distance from leaf nodes in a binary tree
- Count all subtrees having the same value of nodes in a binary tree
- Find the maximum difference between a node and its descendants in a binary tree
- Construct a binary tree from inorder and preorder traversal
- Construct a binary tree from inorder and postorder traversals
- Construct a binary tree from inorder and level order sequence
- Construct a full binary tree from the preorder sequence with leaf node information
- Construct a full binary tree from a preorder and postorder sequence
- Find postorder traversal of a binary tree from its inorder and preorder sequence
- Set next pointer to the inorder successor of all nodes in a binary tree
- Find preorder traversal of a binary tree from its inorder and postorder sequence
- Clone a binary tree with random pointers
- Threaded Binary Tree – Overview and Implementation
- Determine if a binary tree satisfies the height-balanced property of a red–black tree
- Construct an ancestor matrix from a binary tree
- Find all possible binary trees having the same inorder traversal
- Perform boundary traversal on a binary tree
- Check if each node of a binary tree has exactly one child
- Evaluate a Binary Expression Tree
- Construction of an expression tree
- Fix children-sum property in a binary tree
- Create a mirror of an m–ary tree
- Print a two-dimensional view of a binary tree
- Determine whether a given binary tree is a BST or not
- Fix a binary tree that is only one swap away from becoming a BST
- Find the size of the largest BST in a binary tree
- Construct a Cartesian tree from an inorder traversal
- Calculate the height of a binary tree with leaf nodes forming a circular doubly linked list
- Link nodes present in each level of a binary tree in the form of a linked list
- Extract leaves of a binary tree into a doubly-linked list
- Find the vertical sum of a binary tree
- In-place convert a binary tree to a doubly-linked list
- Check whether the leaf traversal of given binary trees is the same or not
- Efficiently print all nodes between two given levels in a binary tree
- Calculate the height of a binary tree
- Delete a binary tree
- Level order traversal of a binary tree
- Spiral order traversal of a binary tree
- Reverse level order traversal of a binary tree
- Print left view of a binary tree
- Find the next node at the same level as the given node in a binary tree
- Print diagonal traversal of a binary tree
- Invert Binary Tree
- Convert a binary tree into a doubly-linked list in spiral order
- Check if a binary tree is a min-heap or not
- Invert alternate levels of a perfect binary tree
- Perform vertical traversal of a binary tree
- Compute the maximum number of nodes at any level in a binary tree
- Print right view of a binary tree
- Find the minimum depth of a binary tree
- Depth-First Search (DFS) vs Breadth-First Search (BFS)
- Print nodes of a binary tree in vertical order
- Find k’th smallest and k’th largest element in a BST
- Convert a binary tree to BST by maintaining its original structure
- Remove nodes from a BST that have keys outside a valid range
- Find a pair with the given sum in a BST
- Find k’th smallest node in a Binary Search Tree (BST)
- Update every key in a BST to contain the sum of all greater keys
- Check if a given sequence represents the preorder traversal of a BST
- Build a Binary Search Tree from a postorder sequence
- Build a Binary Search Tree from a preorder sequence
- Count subtrees in a BST whose nodes lie within a given range
- Print complete Binary Search Tree (BST) in increasing order
- Merge two BSTs into a doubly-linked list in sorted order
- Construct a height-balanced BST from an unbalanced BST
- Construct a height-balanced BST from a sorted doubly linked list
- Find a triplet with the given sum in a BST
- Convert a Binary Search Tree into a Min Heap
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)
