The diameter of a binary tree is the length of the longest path between any two nodes. The path may or may not pass through the root.
Key insight: at each node, the diameter through that node = height of left subtree + height of right subtree.
left_height + right_heightThis is a pattern where you return one thing (height) but track another (diameter) — very common in tree problems.
diameter_of_binary_tree(root)
3
diameter_of_binary_tree(TreeNode(1, TreeNode(2)))
1