104_二叉树的最大深度
2026/4/17小于 1 分钟
104_二叉树的最大深度
简单Java
class Solution {
public int maxDepth(TreeNode root) {
if(root != null){
return 1 + Math.max(maxDepth(root.left),maxDepth(root.right));
}
return 0;
}
}Python
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))