Diameter of Binary Tree

Medium Problems DSA practice problem on Onlearn.

Difficulty: medium.

Topics: Diameter of a Binary Tree: Problem, Brute Force and Optimized Solutions, Binary Tree, Traversal, Recursion, Tree Construction, Height Calculation, Postorder Traversal, Optimization, Time Complexity, Space Complexity, complexity analysis, tree properties, binary tree, postorder traversal, recursion, Tree Height & Diameter.

Problem Statement: Given the root of a binary tree, return the length of its diameter. The diameter of a binary tree is the longest distance between any two nodes of that tree. This path may or may not pass through the root. Input Specification: The input consists of a binary tree represented by its root node. Each node in the tree follows the structure: class Node { int data; Node left; Node right; } Output Specification: Return an integer representing the diameter of the binary tree. Constraints: The number of nodes in the tree is in the range [1, 10^4]. The values of the nodes are between [0, 100]. Sample Test Cases: Input (tree representation): 1 / \ 2 3 / \ 4 5 Output: 3 Explanation: The diameter is the path [4, 2, 1, 3] or [5, 2, 1, 3]. Input (tree representation): 1 \ 2 / \ 3 4 / \ 5 6 / \ 7 8 Output: 5 Explanation: The diameter is the path [7, 5, 3, 6, 8]. Difficulty Level: Medium