此题看题解
class Solution:
def __init__(self):#self
self.maxnum=0
def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
self.depth(root)
return self.maxnum
def depth(self,root):#self
if not root:return 0
l=self.depth(root.left)
r=self.depth(root.right)
self.maxnum=max(self.maxnum,l+r)
return max(l,r)+1