本文共 394 字,大约阅读时间需要 1 分钟。
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代价是会进行无谓的递归
转载地址:http://yumji.baihongyu.com/