×

# Longest Path in an undirected tree

 0 I used two DFS to approach the solution. But I'm getting run time error. Can anyone help me with this? Link to the problem Link to source code asked 16 Feb, 20:18 2★spd123 3●2 accept rate: 0%

 0 Your solution is accepted after correcting some logical error. there were some logical errors in your solution which were giving Segfault. int *dist;  Here dist is an integer pointer so sizeof(dist) will give either $8$ byte or $4$ byte depending on architecture. So the line memset(dist,-1,sizeof(dist));  is making all the worst. Because it is setting only first 4 or 8 bytes as -1 but the array size is larger than this. Now if you replace this with memset(dist,-1,sizeof(int) * (N + 1));  then this will work. Edited code link LongestPathCorrected answered 17 Feb, 15:05 2★brij_raj 76●7 accept rate: 10% Thanks @brij_raj (18 Feb, 11:48) spd1232★
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×734