Diameter of a Binary Tree

Problem Summary

Main Concepts Used

Time & Space Complexity

Code

class Solution:
    def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
        result = 0

        def helper(root):
            nonlocal result
            if not root:
                return 0
            pathLeft = helper(root.left)
            pathRight = helper(root.right)
            result = max(result, pathLeft + pathRight)
            return 1 + max(pathLeft, pathRight)

        helper(root)
        return result

Common Mistakes / Things I Got Stuck On