BackhardDynamic Programming

Maximum Tree Diameter Solution

Problem Statement

Given a connected, undirected tree with N nodes, find the maximum diameter, defined as the longest simple path between any two nodes.

Example 1
Input
Tree: [1, 2, 3, 4] with edges: [(1,2), (2,3), (3,4)]
Output
3

Explanation: The longest path is from node 1 to node 4 with a length of 3: 1 -> 2 -> 3 -> 4

Constraints

  • The tree is connected and undirected.
  • The tree has N nodes, where 2 <= N <= 1000.
  • Each node has a unique value between 1 and 1000.
  • The tree edges are given as a list of pairs (u, v), where u and v are node values.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free