Untitled

mail@pastecode.io avatar
unknown
python
7 months ago
600 B
3
Indexable
Never
class Solution:
    def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:

        if not root:
            return 0

        self.max_diameter = 0

        def helper(root):

            left = 0
            right = 0

            if root.left:
                left = helper(root.left)
            
            if root.right:
                right = helper(root.right)
            
            self.max_diameter = max(self.max_diameter, left + right)

            return 1 + max(left, right)
        
        helper(root)
        
        return self.max_diameter