# 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:
result = [1]
flag = self.Depth(root, result, 1)
if flag is False:
return result[0] - 1
return result[0]
def Depth(self, root, result, level):
if root is None:
return False
if level > result[0]:
result[0] = level
self.Depth(root.left, result, level + 1)
self.Depth(root.right, result, level + 1)
return True
常规递归法,详情见二叉树通用遍历方法