K12教育赛事综合服务平台
专注青少年竞赛题库网站
聚乐之家官方网站
下载聚乐之家官方App
def getDepth(root): if root is None: return 0 left_depth = getDepth(root.left) right_depth = getDepth(root.right) if left_depth < right_depth: return right_depth + 1 else: return left_depth + 1
该代码可用于求解二叉树的深度
代码中函数 getDepth( ) 的参数 root 表示根节点,非根节点不可以作为参数
代码中函数 getDepth( ) 采用了递归方法
代码中函数 getDepth( ) 可用于求解各种形式的二叉树深度,要求该二叉树节点至少有 left 和 right 属性