-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquestion.py
More file actions
55 lines (46 loc) · 1.03 KB
/
question.py
File metadata and controls
55 lines (46 loc) · 1.03 KB
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
50
51
52
53
54
55
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/8/1 13:17
# @Author : cancan
# @File : question.py
# @Function : N叉树的最大深度
"""
Question:
给定一个N叉树,找到其最大深度。
最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。
例如,给定一个 3叉树 :
1
/ | \
3 2 4
/ \
5 6
我们应返回其最大深度,3。
Note:
1.树的深度不会超过 1000。
2.树的节点总不会超过 5000。
"""
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution(object):
def maxDepth(self, root):
"""
:type root: Node
:rtype: int
"""
count = 0
if not root:
return count
else:
t = [root]
while t:
count += 1
temp = []
for node in t:
temp.extend(node.children)
t = temp
return count