↓Skip to main content
  1. LeetCode/

LeetCode 3787: Find Diameter Endpoints of a Tree

·2 mins· ·
LeetCode Medium Tree Breadth-First-Search Graph
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-10-22

  • Submissions: Accepted (first try)

Approach
#

Mark every node that is an endpoint of some diameter. A tree can have many diameters, so finding one is not enough.

First find one diameter with two BFS passes (Tree Template 5):

  1. BFS from any node (0); the farthest node A is an endpoint of some diameter.
  2. BFS from A; the farthest node B is the other end, and dist(A, B) is the diameter D.

Key fact: for every node in a tree, a farthest node can be taken to be A or B. So node i’s eccentricity is max(dist(A, i), dist(B, i)).

Node i is an endpoint of some diameter exactly when its eccentricity equals D. So BFS from A and from B and check max(dA[i], dB[i]) == D.

My Attempts
#

Accepted on the first try.

Solution
#

class Solution:
    def findSpecialNodes(self, n: int, edges: List[List[int]]) -> str:
        graph = [[] for _ in range(n)]
        for u, v in edges:
            graph[u].append(v)
            graph[v].append(u)

        def bfs(s):
            dist = [-1] * n
            dist[s] = 0
            queue = deque([s])
            while queue:
                node = queue.popleft()
                for nei in graph[node]:
                    if dist[nei] < 0:
                        dist[nei] = dist[node] + 1
                        queue.append(nei)
            return dist

        d0 = bfs(0)
        a = d0.index(max(d0))
        dA = bfs(a)
        b = dA.index(max(dA))
        dB = bfs(b)
        diameter = dA[b]
        return "".join("1" if max(dA[i], dB[i]) == diameter else "0" for i in range(n))

Complexity
#

Three BFS passes: time O(n), space O(n).

Mistakes to Avoid
#

  • The diameter is not unique; marking only A and B misses endpoints of other diameters.
  • With a single node the diameter is 0 and that node is an endpoint; the formula still holds.

Interview Explanation
#

Two BFS passes give one diameter’s ends A and B and its length D. In a tree, a farthest node from any node can be chosen as A or B, so each node’s eccentricity is the larger of its distances to A and B; the nodes where that equals D are exactly the diameter endpoints. Three BFS passes, O(n) time.

Related

LeetCode 314: Binary Tree Vertical Order Traversal
·2 mins
LeetCode Medium Hash-Table Tree Depth-First-Search Breadth-First-Search Sorting Binary-Tree
LeetCode 314 note: BFS with a column index; BFS is what guarantees top-to-bottom within a column and left-to-right within a row
LeetCode 116: Populating Next Right Pointers in Each Node
·2 mins
LeetCode Medium Linked-List Tree Depth-First-Search Breadth-First-Search
LeetCode 116 note: in a perfect binary tree, walk each level through the next pointers already set on the level above: left.next = right and right.next = next.left, with O(1) extra space
LeetCode 117: Populating Next Right Pointers in Each Node II
·2 mins
LeetCode Medium Linked-List Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 117 note: connect next pointers level by level with BFS; for a non-perfect tree, looking only at the neighbour’s children breaks the chain
LeetCode 133: Clone Graph
·1 min
LeetCode Medium Hash-Table Depth-First-Search Breadth-First-Search Graph
LeetCode 133 note: during DFS map each old node to its copy and return the existing copy when revisited, avoiding infinite recursion; an empty graph returns None
LeetCode 102: Binary Tree Level Order Traversal
·2 mins
LeetCode Medium Tree Breadth-First-Search Binary-Tree
LeetCode 102 note: BFS one whole level at a time, building the next level from the current one; an empty tree returns an empty list
LeetCode 1448: Count Good Nodes in Binary Tree
·1 min
LeetCode Medium Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 1448 note: pass the path maximum down during DFS; a node is good when its value is at least that maximum