码迷,mamicode.com
首页 > 其他好文 > 详细

[Lintcode]97. Maximum Depth of Binary Tree/[Leetcode]104. Maximum Depth of Binary Tree

时间:2019-02-14 20:52:17      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:别人   The   like   its   root   empty   href   ini   height   

97. Maximum Depth of Binary Tree/104. Maximum Depth of Binary Tree

  • 本题难度: Easy
  • Topic: Binary Tree

Description

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.

Example
Example 1:
Input: tree = {}
Output: 0

Explanation:
The height of empty tree is 0.

Example 2:
Input: tree = {1,2,3,#,#,4,5}
Output: 3

Explanation:
Like this:
      1  
     / \                
    2  3                
      /  \                
     4   5 

我的代码

"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""

class Solution:
    """
    @param root: The root of binary tree.
    @return: An integer
    """
    def maxDepth(self, root):
        # write your code here
        return 1+max(self.maxDepth(root.left),self.maxDepth(root.right)) if root else 0

别人的代码

def maxDepth(self, root):
    return 1 + max(map(self.maxDepth, (root.left, root.right))) if root else 0

思路
递归

  • 出错
    记得加self

[Lintcode]97. Maximum Depth of Binary Tree/[Leetcode]104. Maximum Depth of Binary Tree

标签:别人   The   like   its   root   empty   href   ini   height   

原文地址:https://www.cnblogs.com/siriusli/p/10376559.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!