Tree Diameter
Updated
Problem statement
You are given an array edges describing an undirected tree. Each entry edges[i] = [u, v] represents an edge between nodes u and v.
The diameter of a tree is the number of edges in its longest path between any two nodes.
Return the diameter of the tree.
Function
treeDiameter(edges: int[][]) → intExamples
Example 1
edges = [[0,1],[0,2]]return = 2The longest path is 1 - 0 - 2, which contains 2 edges.
Example 2
edges = [[0,1],[1,2],[2,3],[1,4],[4,5]]return = 4One longest path is 3 - 2 - 1 - 4 - 5, which contains 4 edges.
Example 3
edges = []return = 0An empty edge list represents a one-node tree, whose longest path has 0 edges.
More Qualcomm Phone Screen questions
Similar questions at other companies
- All Nodes Distance K in a Binary TreeAmazon · PHONE SCREEN
- Frog Position After T SecondsInMobi · PHONE SCREENInMobi interview questions
- Base Unit ConversionsAdobe · OAAdobe interview questions
- Diameter of an Acyclic Undirected GraphSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Maximum Data Transfer TimeMicrosoft · OAMicrosoft interview questions
- Longest Transfer Time in a Server TreeRippling · OARippling interview questions
- Special Diameter EndpointsAgoda · OAAgoda interview questions
- Detonate the Maximum BombsZomato / Eternal · PHONE SCREENZomato / Eternal interview questions
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Evaluate DivisionApple · PHONE SCREENApple interview questions