如题,求二叉树的最大深度
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def __init__(self):
self.ans = 0
def maxDepth(self, root: TreeNode) -> int:
def countLevel(tree: TreeNode, level: int):
if tree is None:
return
if level > self.ans:
self.ans = level
countLevel(tree.left, level + 1)
countLevel(tree.right, level + 1)
countLevel(root, 1)
return self.ans