Graph Theory37 sections · 1633 units
Open in Course

LeetCode 543 Diameter of Binary Tree - Problem Statement

LeetCode 543

You are given a binary tree. Return the diameter: the length of the longest path between any two nodes. The path may or may not pass through the root. This is the LeetCode version.

Unlike the CSES problem which uses adjacency lists, here you have TreeNode pointers with left/right children. The DP approach fits naturally. For each node, combine left and right heights to check if a path through this node is the longest.