-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path104.py
34 lines (25 loc) · 791 Bytes
/
104.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Definition for a binary tree node.
import re
import collections
import itertools
import heapq
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: TreeNode) -> int:
if not root:
return 0
q = collections.deque([root])
depth = 0
while q:
depth += 1
for _ in range(len(q)):
cur_root = q.popleft()
if cur_root.left:
q.append(cur_root.left)
if cur_root.right:
q.append(cur_root.right)
return depth