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
代价将是不必要的递归
版权声明:本文博客原创文章,博客,未经同意,不得转载。