Here is the interview question prompt, presented for reference.
Given an undirected graph, can you see if it's a tree? If so, return true
and false otherwise.
An undirected graph
is a tree based on the following conditions: first, there cannot be a cycle. Second, the graph
must be connected.
This is an example of a graph
that is a tree:
1 - 2
|
3 - 4
This one is an example of one that is not:
1
/ \
2 - 3
/
4
You'll be given two parameters: n
for number of nodes, and a multidimensional array of edges
like such: [[1, 2], [2, 3]]
, each pair representing the vertices connected by the edge.
100000
100000
O(|V| + |E|)
where |V|
be the number of nodes n
in the graph and |E|
to be the number of edges in the graphO(|V|)
You can see the full challenge with visuals at this link.
Challenges • Asked almost 7 years ago by Jake from AlgoDaily
This is the main discussion thread generated for Is This Graph a Tree.