importcollectionsfromtypingimportOptional# Definition for a binary tree node.
classTreeNode:def__init__(self,val=0,left=None,right=None):self.val=valself.left=leftself.right=rightclassSolution:defmaxDepth(self,root:Optional[TreeNode])->int:ifrootisNone:return0dq=collections.deque([root])depth=0whiledq:depth+=1for_inrange(len(dq)):cur_root=dq.popleft()ifcur_root.left:dq.append(cur_root.left)ifcur_root.right:dq.append(cur_root.right)returndepth
Leave a comment