您的位置:首页 > 职场人生

【剑指offer】面试题39:二叉树的深度

2014-07-08 00:32 357 查看
def TreeDepth1(root):
if None == root:
return 0
if None == root.left and None == root.right:
return 1
leftDepth = 0; rightDepth = 0
if root.left:
leftDepth = TreeDepth(root.left)
if root.right:
rightDepth = TreeDepth(root.right)
return max(leftDepth, rightDepth) + 1

简洁点的

ef TreeDepth(root):
if None == root:
return 0
return max(TreeDepth(root.left), TreeDepth(root.right)) + 1


代价是会进行无谓的递归
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: