-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path559.py
49 lines (46 loc) · 1.33 KB
/
559.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
__________________________________________________________________________________________________
sample 600 ms submission
"""
# Definition for a Node.
class Node:
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution:
def maxDepth(self, root: 'Node') -> int:
def dfs(root):
if not root: return 0
if not root.children: return 1
height = list()
for node in root.children:
height.append(dfs(node))
return max(height) + 1
d = dfs(root)
return d
__________________________________________________________________________________________________
sample 17320 kb submission
"""
# Definition for a Node.
class Node:
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution:
def maxDepth(self, root: 'Node') -> int:
global ans
ans=0
n=1
if not root:
return ans
def f(a,n):
global ans
if a.children==[]:
ans=max(ans,n)
return
for i in a.children:
f(i,n+1)
f(root,n)
return ans
__________________________________________________________________________________________________