Problem:
Diameter of a Tree

Given a tree T with non-negative weight, find the diameter of the tree.

The diameter of a tree is the maximum distance between two nodes in a tree.

Input

n
s1 t1 w1
s2 t2 w2
:
sn-1 tn-1 wn-1

The first line consists of an integer n which represents the number of nodes in the tree. Every node has a unique ID from 0 to n-1 respectively.

In the following n-1 lines, edges of the tree are given.

si and ti represent end-points of the i-th edge (undirected) and wi represents the weight (distance) of the i-th edge.

Output

Print the diameter of the tree in a line.

Constraints

1 ≤ n ≤ 100,000

0 ≤ wi ≤ 1,000

Sample Input 1

4
0 1 2
1 2 1
1 3 3

Sample Output 1

5

Sample Input 2

4
0 1 1
1 2 2
2 3 4

Sample Output 2

7

Solution in JAVA (judge verdict: Wrong Answer)

import java.util.ArrayList;
import java.util.Scanner;
 
public class Main {
 
    static long x = 0;
    public static void main(String[] args) {
    	try {
	        Scanner sc = new Scanner(System.in);
	          int n= sc.nextInt();
	//        cin >> n;
	          
	          ArrayList<node> tree = new ArrayList<>();
	          for (int i = 0; i < n; i++) {
	              tree.add(new node());
	          }
	          
	          for (int i = 0, s, t, w; i < n - 1; i++) {
	//          cin >> s >> t >> w;
	            s = sc.nextInt();
	            t = sc.nextInt();
	            w = sc.nextInt();
	          
	            tree.get(s).next.add(new edge(w, t));
	            tree.get(t).next.add(new edge(w, s));
	          }
	          
	          solve(tree, 0);
	//        cout << tree.get(0).diameter << endl;
	         System.out.println(tree.get(0).diameter);
    	} catch (StackOverflowError e) {
    		System.out.println(-2);
    	}
    }
 
     
    static class edge {
          int weight;
          int nodeIdx;
          edge(int w, int i) {
            weight = w;
            nodeIdx = i;
          }
        }
          
    static class node {
          ArrayList<edge> next = new ArrayList<>();
          int height;
          int diameter;
          boolean visited;
          
          node() {
            height = 0;
            diameter = 0;
            visited = false;
          }
        }
          
        static void solve(ArrayList<node> tree, int idx) {
          //cout << "called for node: " << idx << endl;
          // if (tree.get(idx).visited) return;
          tree.get(idx).visited = true;
          
          int bestHeight  = 0, bestHeight2 = 0, bestDiameter = 0;
          
          for (int i = 0; i < tree.get(idx).next.size(); i++) {
            int nextIdx = tree.get(idx).next.get(i).nodeIdx;
            if (tree.get(nextIdx).visited) continue;
          
            //cout << idx << " next-> " << nextIdx << endl;
            solve(tree, nextIdx);
          
            bestDiameter = Math.max(bestDiameter, tree.get(nextIdx).diameter);
            int h = tree.get(nextIdx).height + tree.get(idx).next.get(i).weight;
          
            if (h > bestHeight) {
              bestHeight2 = bestHeight;
              bestHeight = h;
            } else if (h > bestHeight2) {
              bestHeight2 = h;
            }
          }
          
          tree.get(idx).height = bestHeight;
          tree.get(idx).diameter = Math.max(bestDiameter, bestHeight + bestHeight2);
          //cout << "Node: " << idx << " H: " << tree.get(idx).height << " D: " << tree.get(idx).diameter << " bestDiameter: " << bestDiameter << endl << endl;
        }
}