【剣指offer】面接問題39:二叉木の深さ
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
代価は無駄な再帰をする