Given a binary tree, find its maximum depth.

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

求给定的二叉树的深度。

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution(object):
    def maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if root == NULL:
            return 0
        return 1+max(maxDepth(root.left)+maxDepth(root.right))
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,769评论 0 33
  • 目录 简书的 markdown 都不支持 [TOC] 语法……我就不贴目录了。下面按照类别,列出了29道关于二叉树...
    被称为L的男人阅读 3,379评论 0 8
  • 你是什么时候意识到你无论怎样努力都比不过别人的。 是在你不分昼夜奋战刷题,一遍遍演算、重复背诵,仍是在高考的千军万...
    Charon阿屎爱搞笑阅读 225评论 0 0
  • 文静 雨一路狂奔, 只为思太久,念太执。 好急,好急, 就这样扎入妈妈的怀里。 大地用干裂的手抚摸雨滴, 我也想你...
    文静_5c0c阅读 338评论 4 10
  • 公司楼顶有个天台,有一天午休闲着没事跑上去,正看到老郭站在护栏网前望着外面抽烟,听到我的声响,转过头来 “你怎么也...
    王叫兽_邢敏阅读 413评论 0 1