# 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 = rightclass Solution: def maxDepth(self, root: Optional[TreeNode]) -> int: countleft = 0 countright = 0 def countDepth(root:TreeNode): if root == None: return 0 else: countleft = countDepth(root.left) countright = countDepth(root.right) if countleft >= countright: countleft += 1 return countleft else: countright += 1 return countright return countDepth(root)
结果: