Buggy python submission (verdict: Runtime Error):
while 1:
    n = input()
    if n == 0:
        break
    G = [[] for i in xrange(n)]
    for i in xrange(n-1):
        s, t, w = map(int, raw_input().split())
        G[s].append((t, w))
        G[t].append((s, w))

    def dfs(s, prev):
        if prev != -1 and len(G[s]) == 1:
            return (0, s)
        res = (0, -1)
        for t, w in G[s]:
            if t == prev:
                continue
            cost, v = dfs(t, s)
            res = max(res, (cost + w, v))
        return res
    co, t = dfs(0, -1)
    if t != -1:
        print dfs(t, -1)[0]
    else:
        print 0

Fixed submission:
N = int(input())
G = [[] for i in range(N)]
for i in range(N-1):
    s, t, w = map(int, input().split())
    G[s].append((t, w))
    G[t].append((s, w))

from collections import deque
def bfs(s):
    dist = [None]*N
    que = deque([s])
    dist[s] = 0
    while que:
        v = que.popleft()
        d = dist[v]
        for w, c in G[v]:
            if dist[w] is not None:
                continue
            dist[w] = d + c
            que.append(w)
    d = max(dist)
    return dist.index(d), d
u, _ = bfs(0)
v, d = bfs(u)

print(d)

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)
while 1:
    n = input()
    if n == 0:
        break
    G = [[] for i in xrange(n)]
    for i in xrange(n-1):
        s, t, w = map(int, raw_input().split())
        G[s].append((t, w))
        G[t].append((s, w))

    def dfs(s, prev):
        if prev != -1 and len(G[s]) == 1:
            return (0, s)
        res = (0, -1)
        for t, w in G[s]:
            if t == prev:
                continue
            cost, v = dfs(t, s)
            res = max(res, (cost + w, v))
        return res
    co, t = dfs(0, -1)
    if t != -1:
        print dfs(t, -1)[0]
    else:
        print 0

Fixed submission:
N = int(input())
G = [[] for i in range(N)]
for i in range(N-1):
    s, t, w = map(int, input().split())
    G[s].append((t, w))
    G[t].append((s, w))

from collections import deque
def bfs(s):
    dist = [None]*N
    que = deque([s])
    dist[s] = 0
    while que:
        v = que.popleft()
        d = dist[v]
        for w, c in G[v]:
            if dist[w] is not None:
                continue
            dist[w] = d + c
            que.append(w)
    d = max(dist)
    return dist.index(d), d
u, _ = bfs(0)
v, d = bfs(u)

print(d)

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)
n = input()
G = [[] for i in xrange(n)]
for i in xrange(n-1):
    s, t, w = map(int, raw_input().split())
    G[s].append((t, w))
    G[t].append((s, w))

def dfs(s, prev):
    if prev != -1 and len(G[s]) == 1:
        return (0, s)
    res = (0, -1)
    for t, w in G[s]:
        if t == prev:
            continue
        cost, v = dfs(t, s)
        res = max(res, (cost + w, v))
    return res
co, t = dfs(0, -1)
if t != -1:
    print dfs(t, -1)[0]
else:
    print 0

Fixed submission:
N = int(input())
G = [[] for i in range(N)]
for i in range(N-1):
    s, t, w = map(int, input().split())
    G[s].append((t, w))
    G[t].append((s, w))

from collections import deque
def bfs(s):
    dist = [None]*N
    que = deque([s])
    dist[s] = 0
    while que:
        v = que.popleft()
        d = dist[v]
        for w, c in G[v]:
            if dist[w] is not None:
                continue
            dist[w] = d + c
            que.append(w)
    d = max(dist)
    return dist.index(d), d
u, _ = bfs(0)
v, d = bfs(u)

print(d)

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)
n = input()
G = [[] for i in xrange(n)]
for i in xrange(n-1):
    s, t, w = map(int, raw_input().split())
    G[s].append((t, w))
    G[t].append((s, w))

def dfs(s, prev):
    if prev != -1 and len(G[s]) == 1:
        return (0, s)
    res = (0, s)
    for t, w in G[s]:
        if t == prev:
            continue
        cost, v = dfs(t, s)
        res = max(res, (cost + w, v))
    return res
co, t = dfs(0, -1)
print dfs(t, -1)[0]

Fixed submission:
N = int(input())
G = [[] for i in range(N)]
for i in range(N-1):
    s, t, w = map(int, input().split())
    G[s].append((t, w))
    G[t].append((s, w))

from collections import deque
def bfs(s):
    dist = [None]*N
    que = deque([s])
    dist[s] = 0
    while que:
        v = que.popleft()
        d = dist[v]
        for w, c in G[v]:
            if dist[w] is not None:
                continue
            dist[w] = d + c
            que.append(w)
    d = max(dist)
    return dist.index(d), d
u, _ = bfs(0)
v, d = bfs(u)

print(d)