# 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 = rightclassSolution:defdiameterOfBinaryTree(self,root:Optional[TreeNode])->int:# left-most node VS right-most node# recall in order traversaldiameter=0defdfs(node):# base caseifnotnode:return0nonlocaldiameter# recursive call to the left and right pathleft_path=dfs(node.left)right_path=dfs(node.right)# update the diameterdiameter=max(diameter,left_path+right_path)returnmax(left_path,right_path)+1dfs(root)returndiameter