具体问题见这里
代码
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
"""
depth = 0
if root == None:
return depth
if root.left == root.right == None:
return 1
left = self.maxDepth(root.left) + 1
right = self.maxDepth(root.right) + 1
return left if left >= right else right
def computeTreeDepth(self,root):
depth = 0
if root == None:
return depth
if root.left == root.right == None:
return 1
if self.computeTreeDepth(root.left) >= self.computeTreeDepth(root.right):
depth = self.computeTreeDepth(root.left) + 1
else:
depth = self.computeTreeDepth(root.right) + 1
return depth
computeTreeDepth是第一次写的代码,结果超时,原因可能是遍历的次数太多了,结果超时不能通过
maxDepth函数虽然通过了但是看了下时间比较长,换了java代码看了看比python时间更长.....