美文网首页LeetCode
104. 二叉树的最大深度

104. 二叉树的最大深度

作者: 凌霄文强 | 来源:发表于2019-03-10 16:08 被阅读0次

题目描述

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例:
给定二叉树 [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回它的最大深度 3 。

知识点

二叉树、递归


Qiang的思路

递归得到左右子树深度。

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def maxDepth(self, root: TreeNode) -> int:
        if root==None:
            return 0
        left=self.maxDepth(root.left)+1
        right=self.maxDepth(root.right)+1
        return left if left>right else right

作者原创,如需转载及其他问题请邮箱联系:lwqiang_chn@163.com
个人网站:https://www.myqiang.top

相关文章

网友评论

    本文标题:104. 二叉树的最大深度

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