题解 | #二叉树的深度#

二叉树的深度

https://www.nowcoder.com/practice/435fb86331474282a3499955f0a41e8b

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def TreeDepth(self, pRoot):
        # write code here
        # 如果为空树
        if pRoot == None:
            return 0
        # 分别记录左子树 和 右子树的高度
        l = self.TreeDepth(pRoot.left)
        r = self.TreeDepth(pRoot.right)

        if l >= r:
            return l + 1
        else:
            return r + 1

#23届找工作求助阵地#
全部评论

相关推荐

代码飞升:别用口语,后端就写后端,前端就写前端,最后别光后悔
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务