class Solution(object):
def maxDepth(self, root):
""" :type root: TreeNode :rtype: int """
if root is None:
return 0
if root.left is None and root.right is None:
return 1
elif root.left is None:
return self.maxDepth(root.right) + 1
elif root.right is None:
return self.maxDepth(root.left) + 1
else:
depth_left = self.maxDepth(root.left)
depth_right = self.maxDepth(root.right)
if depth_left > depth_right:
return depth_left + 1
else:
return depth_right + 1
Runtime: 64 ms