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):
return 1 + max(map(self.maxDepth, (root.left, root.right))) if root else 0
本文介绍了一种求解二叉树最大深度的算法。该算法通过递归方式遍历二叉树的所有节点,找到从根节点到最远叶子节点的最长路径上的节点数。
749

被折叠的 条评论
为什么被折叠?



