题解 | #二叉树的深度#

二叉树的深度

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))

全部评论

相关推荐

兄弟找我内推呗:兄弟你问问他们饭菜能打包吗
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务