LeetCode 104 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.

Note: A leaf is a node with no children.

题目分析及思路

给定一棵二叉树,要求返回它的最大深度。最大深度是指在从根结点到最远叶结点的路径上的结点数。可以使用递归的方法,条件为结点为空。

python代码

# 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 not root:

return 0

return 1+max(self.maxDepth(root.left), self.maxDepth(root.right))

上一篇:Tomcat的简单归纳总结


下一篇:Android Error:Could not find lottie.jar