<reponame>jimit105/leetcode-submissions
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
        diameter = 0 
        
        def find_longest_path(node):
            nonlocal diameter
            if not node:
                return 0
            
            left_path = find_longest_path(node.left)
            right_path = find_longest_path(node.right)
            
            diameter = max(diameter, left_path+right_path)
            
            return max(left_path, right_path)+1
        
        find_longest_path(root)
        return diameter
        
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
        diameter = 0 
        
        def find_longest_path(node):
            nonlocal diameter
            if not node:
                return 0
            
            left_path = find_longest_path(node.left)
            right_path = find_longest_path(node.right)
            
            diameter = max(diameter, left_path+right_path)
            
            return max(left_path, right_path)+1
        
        find_longest_path(root)
        return diameter
# Definition for a binary tree node. # class TreeNode: #     def __init__(self, val=0, left=None, right=None): #         self.val = val #         self.left = left #         self.right = right