I was solving this problem onn spoj Problem. What I am doing is choosing a leaf node as the start point of dfs and counting the max edges at each point that I can get… Is that approach wrong . Here is my code it fails on test case 5 on spoj.

```
#include<bits/stdc++.h>
using namespace std;
list <int> adj[10001];
bool visited[10001];
int dfs(int start)
{
list <int>::iterator ii;
int high=0;
for(ii=adj[start].begin();ii!=adj[start].end();ii++)
if(!visited[*ii])
{
visited[*ii]=true;
high=max(high,dfs(*ii)+1);
}
return high;
}
int main()
{
int V,i,j,k,buff,l,r;
scanf("%d",&V);
if(V==1)
{
scanf("%d%d",&l,&r);
printf("0\n");
exit(0);
}
for(i=1;i<=V-1;i++)
{
scanf("%d%d",&l,&r);
adj[l].push_back(r);
adj[r].push_back(l);
}
for(i=1;i<=V;i++)
{
if(adj[i].size()==1)
break;
}
visited[i]=true;
printf("%d\n",dfs(1));
return 0;
```

}