1. Code
class Solution:
def maxDepth(self, root: 'Node') -> int:
depth = 0
# when root exists
if root:
# depth of the root is 1
depth += 1
queue = deque([(root,depth)])
while queue:
node, dep = queue.popleft()
for e in node.children:
# append node's children and the depth of them to the queue
queue.append((e, dep+1))
# update depth to the deepest child
depth = dep
return depth
2. Result
Runtime : 36 ms(97.88%), Memory usage : 16.3 MB(20.12%)
(Runtime can be different by a system even if it is a same code.)