Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 947c33d

Browse files
committed
update 树的层次遍历
1 parent b1d68d9 commit 947c33d

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

Readme.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,14 +1664,10 @@ tree = Node(1, Node(3, Node(7, Node(0)), Node(6)), Node(2, Node(5), Node(4)))
16641664
```python
16651665

16661666
def lookup(root):
1667-
stack = [root]
1668-
while stack:
1669-
current = stack.pop(0)
1670-
print current.data
1671-
if current.left:
1672-
stack.append(current.left)
1673-
if current.right:
1674-
stack.append(current.right)
1667+
row = [root]
1668+
while row:
1669+
row = [kid for item in row for kid in (item.left, item.right) if kid]
1670+
print(row)
16751671

16761672
```
16771673

0 commit comments

Comments
 (0)