# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: List[float]
"""
if not root:
return 0
else:
return 1+max(self.maxDepth(root.left),self.maxDepth(root.right))
Sol = Solution()
t1 = TreeNode(1)
t1.left = TreeNode(2)
t1.right = TreeNode(3)
print Sol.maxDepth(t1)
0,比较简洁
1,问题是求一棵树的最大深度,用到了树的递归算法。思想还是比较重要的。