Skip to main content
easyNeetCode 150

Diameter of Binary Tree

Return the diameter (longest path in edges) of a binary tree. Example: [1,2,3,4,5] => 3

Diameter of Binary Tree teaches the post-order trick that powers half of all tree problems: each node returns its height while a side variable tracks the best answer seen. The path that matters rarely runs through the root, so a top-down count fails. Once the height-plus-side-effect pattern clicks, max-path-sum feels familiar.

TreesBFS / DFS

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Typing with Coco is free when you sign in, up to a daily limit. Talking with her out loud comes with the $79 pack. Anyone can open the editor and run their code.

1Diameter of Binary Treeeasy
Based onLeetCode