# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param root TreeNode类 
# @return int整型
#
class Solution:
    def maxDepth(self , root: TreeNode) -> int:
        # write code here
        if not root:
            return 0
        #return 1+max(self.maxDepth(root.left),self.maxDepth(root.right))
        # write code here 利用DFS深度优先
        ans = 0
        s = [(root,1)]
        while len(s):
            p = s.pop()
            ans = max(ans,p[1])
            if p[0].left:
                s += [(p[0].left,p[1]+1)]
            if p[0].right:
                s += [(p[0].right,p[1]+1)]
        return ans
        # write code here 利用BFS广度优先来做