Problem Statement

Given the root of a binary tree, return the length of the diameter of the tree.

The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.

The length of a path between two nodes is represented by the number of edges between them.

Example 1:

Input: root = [1,2,3,4,5]
Output: 3
Explanation: 3is the length of the path [4,2,1,3] or [5,2,1,3].

Example 2:

Input: root = [1,2]
Output: 1

Problem Link

Diameter of Binary Tree - LeetCode

Reference Video

https://www.youtube.com/watch?v=zmPj_Ee3B8c&list=PL_z_8CaSLPWekqhdCPmFohncHwz8TY2Go&index=48&ab_channel=AdityaVerma

Code

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int diameterOfBinaryTree(TreeNode* root) {
        
        int res = 0;
       solve(root,res);
       return res-1;// return res; if diameter is defined in terms of no. of nodes 
    }
    int solve(TreeNode *root,int &res) {
        
        if(!root)
            return 0;
        
        int ldiameter = solve(root->left,res);
        int rdiameter = solve(root->right,res);
        
        int temp = 1+ max(ldiameter,rdiameter); //passes to the upper root
        int ans = max(temp,1+ldiameter+rdiameter);// calculated for each node
        res = max(res,ans);// calculated for the entire tree
        
        return temp;
    }
};
// Time Complexity : O(n)
// Space Complexity : O(h) where h = height of tree
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int diameterOfBinaryTree(TreeNode* root) {
        
        int ans = 0;
        solve(root,ans);
            return ans-1;
        
    }
    
    int solve(TreeNode *root,int &ans)
    {
        if(!root)
            return 0;
        
        int lst = solve(root->left,ans);
        int rst = solve(root->right,ans);
        
        int ht = 1+max(lst,rst);
        
        ans = max(ans,1+lst+rst);//max(diameter not passes through current root,diamter passes through current root )
        
        return ht;
    }
};

Striver solution

https://www.youtube.com/watch?v=Rezetez59Nk&ab_channel=takeUforward

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
int maxi;
    int diameterOfBinaryTree(TreeNode* root) {

        maxi = 0;

        solve(root);

        return maxi;

       
        
    }

    int solve(TreeNode* root) {
         if(!root) {
            return 0;
        }

        int lh = solve(root->left);
        int rh = solve(root->right);

        maxi = max(maxi,lh+rh);

        return 1 + max(lh,rh);
    }
};