Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

DFS of an Acyclic Graph Editorial

DSA Editorial, Solution and Code

Practice Problem Link: DFS of an Acyclic Graph

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given an acyclic graph (n-ary tree) compute its pre-order traversal.

The graph has n nodes indexed 0 to n-1. It is given in the form of an adjacency list and you can consider the node indexed 0 to be the root.

Return the pre-order traversal starting at the root.

Approach

  • Start from the root node and add it to the answer array.
  • Now traverse its adjacent nodes and for every node except the parent of the current root node, do the above steps recursively.

Analysis

  • Time Complexity: O(V + E)
  • Auxiliary Space Complexity: O(V)

Where V is the number of nodes and E is the number of edges.

Implementation

C++
void dfsUtil(int root, int parent, vector<vector<int>> &adjList, vector<int> &traversal) {
	traversal.push_back(root);
	for(int i = 0; i < adjList[root].size(); i++) {
		int node = adjList[root][i];
		if (node != parent) {
			dfsUtil(node, root, adjList, traversal);
		}
	}
}
vector<int> dfs(vector<vector<int>> adjList) {
	vector<int> traversal;
    dfsUtil(0, - 1, adjList, traversal);
	return traversal;
}
Java
class Solution {
	void dfsUtil(int root, int parent, ArrayList<Integer>[] adjList, ArrayList<Integer> traversal) {
		traversal.add(root);
		for(int i = 0; i < adjList[root].size(); i++) {
			int node = adjList[root].get(i);
			if (node != parent) {
				dfsUtil(node, root, adjList, traversal);
			}
		}
	}
    ArrayList<Integer> dfs(ArrayList<Integer>[] adjList) {
        ArrayList<Integer> traversal = new ArrayList<Integer>();
		dfsUtil(0, - 1, adjList, traversal);
		return traversal;
    }
}
Related Content
Adjacency List to Adjacency Matrix
Adjacency Matrix to Adjacency List
BFS of a Graph
Capture Surrounded Regions
Clone a Graph
DFS of a Cyclic Graph
Edges to Adjacency List
Flood Fill Image
Knight's Journey On A Chessboard
M-Coloring Problem
Number of Islands
Stepping Numbers
Valid Path
Word Search Board
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet