Maximum Depth of Binary Tree - (递归遍历)

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.

For example:
Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its depth = 3.

# 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
        """
        return self.middle(0, root)
        
    def middle(self, depth, root):
        if root == None:
            return depth
        depth += 1
        left = self.middle(depth, root.left)
        right = self.middle(depth, root.right)
        return left if left > right else right
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,778评论 0 33
  • 目录 简书的 markdown 都不支持 [TOC] 语法……我就不贴目录了。下面按照类别,列出了29道关于二叉树...
    被称为L的男人阅读 3,381评论 0 8
  • 刹那即永恒, 山水重重, 记得来时路葱茏, 细雨蒙蒙清风。 笑红尘, 扑扑脂粉为谁浓, 一生, 香丘本无踪, 看我...
    带走我的xz阅读 280评论 0 1
  • *王者荣耀 李白X赵云 今晚赵云将军的屋子内充满了暧昧的味道。 窗帘被拉了起来,月光被挡在窗外,屋内几盏微弱的烛火...
    Nenn阅读 5,037评论 1 10
  • 《清晨》 鸟儿啼 垃圾车和除草机轰鸣儿 这, 清晨的声音 《正午》 太阳天空照, 花儿对我笑, 人们玩着躲猫猫, ...
    无墨水阅读 106评论 0 1