#18

Diameter of Binary Tree

Easy
All Problems

18. Diameter of Binary Tree#543

Easy🌳 TreesGlobal State DFS

Find the length of the longest path between any two nodes in a binary tree.

Examples

Example 1:
Input: [[1,2,3,4,5]]
Output: 3
Example 2:
Input: [[1,2]]
Output: 1
Example 3:
Input: [[1]]
Output: 0

Why learn this

Builds on Max Depth — teaches tracking global state during recursion.

TreesDFSGlobal State
Python 3Loading Python engine...
Click "Run Code" to test your solution