给定一个二叉树,找出其最大深度。
递归求解:
# 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: int
"""
return self.treeDepth(root)
def treeDepth(self, t):
if t == None:
return 0
ldepth = self.treeDepth(t.left)
rdepth = self.treeDepth(t.right)
return max(ldepth,rdepth)+1