Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

116. Diameter of Binary Tree

EasyDay 34 of 60Trees & BST

Given the root of a binary tree, return the length of its diameter: the number of edges on the longest path between any two nodes. The path does not have to go through the root.

Example 1
Input: root = [1,2,3,4,5]
Output: 3
The path 4, 2, 1, 3 (or 5, 2, 1, 3) has 3 edges.
Example 2
Input: root = [1,2]
Output: 1
Constraints
  • The number of nodes is in the range 1 to 10^4
  • -100 <= Node.val <= 100
Hints
Frequently asked at: Meta, Amazon, Google, Microsoft
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min