-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution.py
59 lines (54 loc) · 1.41 KB
/
solution.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
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
# Created by Bruce yuan on 18-2-7.
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
# BFS
if root is None:
return 0
import queue
q = queue.Queue()
q.put(root)
depth = 0
while not q.empty():
depth += 1
for _ in range(q.qsize()):
item = q.get()
if item.left is not None:
q.put(item.left)
if item.right is not None:
q.put(item.right)
if item.left is None and item.right is None:
return depth
return depth
@staticmethod
def dfs(root):
"""
用可以用非递归的dfs来做吗?暂时没想到
:param root:
:return:
"""
if root is None:
return 0
node = [root]
pass
def recurrent(self, root):
"""
这个超级好理解。
:param root:
:return:
"""
if root is None:
return 0
l = self.recurrent(root.left)
r = self.recurrent(root.right)
return l + r + 1 if l == 0 or r == 0 else min(l, r) + 1