[LeetCode By Python] 104. Maximum Depth of Binary Tree

一、题目

Maximum Depth of Binary Tree

二、解题

二叉树的深度遍历,遍历递归左右子树就可以了。

三、尝试与结果

class Solution(object):
    def maxDepth(self, root):
        if root == None:
            return 0
        if root.left == None and root.right == None:
            return 1
        leftLen = self.maxDepth(root.left)
        rightLen = self.maxDepth(root.right)
        maxLen = leftLen if leftLen>rightLen else rightLen
        return maxLen + 1

结果:AC

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容