Tap here to switch tabs
Problem
Submissions

Problem: Tree Diameter

Tree Diameter asks you to examine an undirected tree given as edge pairs and return the number of edges on the longest path between any two nodes.

Statement

Given an undirected tree with nn nodes labeled from 00 to n−1n - 1, represented by a 2D array edges where edges.length==n−1edges.length == n - 1. Each edges[i]=[ai,bi]edges[i] = [a_i, b_i] denotes an undirected edge between nodes aia_i and bib_i. Your task is to return the diameter of the tree.

The diameter of a tree is the number of edges in the longest path between any two nodes.

Constraints:

  • n ==== edges.length +1+ 1

  • edges[i].length = 2

  • 1 <=<= nn <=<=10310^3

  • 0 <=<= ai,bia_i, b_i << nn

  • aia_i ≠bi\neq b_i

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Tree Diameter

Tree Diameter asks you to examine an undirected tree given as edge pairs and return the number of edges on the longest path between any two nodes.

Statement

Given an undirected tree with nn nodes labeled from 00 to n−1n - 1, represented by a 2D array edges where edges.length==n−1edges.length == n - 1. Each edges[i]=[ai,bi]edges[i] = [a_i, b_i] denotes an undirected edge between nodes aia_i and bib_i. Your task is to return the diameter of the tree.

The diameter of a tree is the number of edges in the longest path between any two nodes.

Constraints:

  • n ==== edges.length +1+ 1

  • edges[i].length = 2

  • 1 <=<= nn <=<=10310^3

  • 0 <=<= ai,bia_i, b_i << nn

  • aia_i ≠bi\neq b_i