求树的深度,与110类似。
<span style="font-family:FangSong_GB2312;font-size:18px;"># 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
"""
t = self.treeDepth(root)
return t
def treeDepth(self,root):
if root is None:
return 0
else:
l = self.treeDepth(root.left)
r = self.treeDepth(root.right)
return max(l,r)+1</span>