Python求二叉树最大深度

继前面Python二叉树递归和遍历

二叉树图

binary tree

1
2
3
4
5
6
def max_depth(node):
if not node:
return 0
ldepth = max_depth(node.left)
rdepth = max_depth(node.right)
return max(ldepth,rdepth) + 1

结果:4