General Syntax

int solve(TreeNode *root,int &res) {
        
        // Base Conditions (may differ from que to que) 
				if(!root)
            return 0;
        
        // Hypothesis (same for all ques)
				int l = solve(root->left,res);
        int r = solve(root->right,res);
        
        // Induction
        int temp = 1+ max(l,r); //passes to the upper root
        int ans = max(temp,____________);// calculated for each node
        res = max(res,ans);// calculated for the entire tree
        
        return temp;
    }

int main(void) {
int res = 0;
solve(root,res);
cout<<res;
}

Variants

Diameter of Binary Tree

Binary Tree Maximum Path Sum (from any node to any node)

Binary Tree Maximum Path Sum (from one leaf node to another leaf node)

Binary Tree Cameras

Largest Independent Set Problem

Count Balanced Binary Trees of Height h