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

一、题目

[LeetCode By Python] 104. Maximum Depth of Binary Tree_第1张图片
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

你可能感兴趣的:([LeetCode By Python] 104. Maximum Depth of Binary Tree)