# Loop in a binary tree represented as an adjacency list

I attained an interview where I was asked a question as below:

You are given with `parent -----> child` relationships i.e. `N1 ---> N2` where `N1` is the parent of `N2`. This is nothing but representing a binary tree in adjacency list form. So I had to find whether there is a loop is present or not.

I have been mulling over this and came up with a solution:

Just need to check individual node i.e. `N1` and try going deep if you see there is a edge coming back to `N1` then print. Else go for next node. But the interviewer told me that it is not very efficient, can somebody help me in finding an efficient solution. Thanks.

You can do in a simple manner:

In a binary tree of N nodes will have N-1 edges. If it has loop then a binary tree having N nodes will have more than N-1 edges.

So you need to calculate the no of nodes and edges (parent->child) if the no_of_nodes == no_of_edges-1 than no loop else has loop.

Hope you understand.