# My easy-to-understand Python Solution

• DFS, just easy to understand^_^

``````class Solution:
# @param {TreeNode} root
# @return {integer}
def maxDepth(self, root):
return self.dfs(root)

def dfs(self, node):
if not node:
return 0
elif (not node.left) and (not node.right):
return 1
else:
return 1 + max(self.dfs(node.left), self.dfs(node.right))``````

• shouldn't this be more concise?

``````class Solution:
def maxDepth(self, root):
if not root:
return 0
elif not root.left and not root.right:
return 1
else:
return 1 + max([self.maxDepth(root.left), self.maxDepth(root.right)])``````

• It can be more concise

``````def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root:
return 0
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
``````

• That has a bug, and after you fix it, it easily gets accepted.

• You've forgotten an else:

• @sahilarrow Huh? No.

• get the depth of root.right and root.left. You currently are checking root.right 2x

• um... thanks a lot

• fix it now, it works

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.