-
Notifications
You must be signed in to change notification settings - Fork 5
/
0559.py
34 lines (31 loc) · 812 Bytes
/
0559.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 Node.
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children
"""
class Solution:
def maxDepth(self, root: 'Node') -> int:
# DFS
if not root:
return 0
depth = 0
for child in root.children:
depth = max(self.maxDepth(child), depth)
return depth + 1
# BFS
import collections
# 特判,不写会报错
if not root:
return 0
q = collections.deque()
q.append(root)
depth = 0
while q:
for _ in range(len(q)):
node = q.popleft()
for child in node.children:
q.append(child)
depth += 1
return depth