题解 | #二叉树的深度#
二叉树的深度
https://www.nowcoder.com/practice/435fb86331474282a3499955f0a41e8b
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param pRoot TreeNode类
# @return int整型
#
class Solution:
def TreeDepth(self , pRoot: TreeNode) -> int:
# write code here
# 二叉树的深度等于1+子树深度中的最大值
if pRoot is None:
return 0
else:
return 1 + max(self.TreeDepth(pRoot.left),self.TreeDepth(pRoot.right))
查看11道真题和解析
