104. 二叉树的最大深度

如题,求二叉树的最大深度

# 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
上一篇:104-django的后台


下一篇:Leetcode-104. 二叉树的最大深度