# 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 classSolution: defmaxDepth(self, root: Optional[TreeNode]) -> int: if root isNone: return0 else: left = self.maxDepth(root.left) right = self.maxDepth(root.right) return max(left, right) + 1
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ funcmaxDepth(root *TreeNode)int { if root == nil { return0 } return max(maxDepth(root.Left), maxDepth(root.Right)) + 1 }
funcmax(a int, b int)int { if a > b { return a } return b }