Skip to content

Commit d1cfb6c

Browse files
committed
树层序遍历补充queue
1 parent 1d21d9f commit d1cfb6c

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

docs/14_树与二叉树/btree.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
11
# -*- coding: utf-8 -*-
22

3-
from collections import deque
4-
5-
6-
class Queue(object):
7-
def __init__(self):
8-
self._items = deque()
9-
10-
def append(self, value):
11-
return self._items.append(value)
12-
13-
def pop(self):
14-
return self._items.popleft()
15-
16-
def empty(self):
17-
return len(self._items) == 0
183

194

205
class BinTreeNode(object):

docs/14_树与二叉树/tree.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,21 @@ btree.preorder_trav(btree.root) # 输出 A, B, D, E, H, C, F, G, I, J
229229
q.append(cur_node.left)
230230
if cur_node.right:
231231
q.append(cur_node.right)
232+
233+
234+
from collections import deque
235+
class Queue(object): # 借助内置的 deque 我们可以迅速实现一个 Queue
236+
def __init__(self):
237+
self._items = deque()
238+
239+
def append(self, value):
240+
return self._items.append(value)
241+
242+
def pop(self):
243+
return self._items.popleft()
244+
245+
def empty(self):
246+
return len(self._items) == 0
232247
```
233248

234249

0 commit comments

Comments
 (0)