Similar Problems

Similar Problems not available

Count number of trees in a forest - Leetcode Solution

LeetCode:  Count number of trees in a forest Leetcode Solution

Difficulty: Unknown

Topics: graphs  

Problem Statement

Given a forest with edge representation, find the number of trees in the forest.

Example Input

edges = {{0, 1}, {0, 2}, {3, 4}, {4, 5}, {5, 6}}

Expected Output

2

Approach

The idea is to apply Depth First Search on every node. If every connected node is visited from one source then increment count by one. If some nodes yet not visited again perform DFS traversal. Return the count.

Count number of trees in a forest Solution Code

1