BackmediumTrees trees medium

Binary Tree Diameter Solution

Problem Statement

Given a binary tree where each node has a unique value, find the diameter of the tree, which is defined as the length of the longest path between any two leaf nodes.

Example 1
Input
[1,null,2,null,null,3,4]
Output
3

Explanation: The longest path is through the nodes with values 3, 2, and 4, which has a length of 3.

Constraints

  • The number of nodes in the tree will not exceed 1000.
  • Each node has a unique value.
  • The tree is represented as a list of node values.
  • The diameter of the tree is the length of the longest path between any two leaf nodes.
  • The path must only traverse through node values.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free