Diameter of a Binary Tree (Leetcode 543)

Technique: FAITH technique of Recursion

Problem Link: https://leetcode.com/problems/diameter-of-binary-tree/

class Solution:

    def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:     
        global ans
        ans=float('-inf')
        
        def height(root):
            global ans
            if(root is None):
                return 0
            lh=height(root.left)
            rh=height(root.right)
            ans=max(ans,lh+rh)
            return max(lh,rh)+1
    
        height(root)
        return ans

Last updated