美文网首页
[LeetCode By Python] 104. Maximu

[LeetCode By Python] 104. Maximu

作者: 乐乐可爱睡觉 | 来源:发表于2016-06-13 15:55 被阅读79次

一、题目

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. Maximu

      本文链接:https://www.haomeiwen.com/subject/fdpydttx.html