检查python 版本
import sys
print(sys.version)
print(sys.version_info )
mac
python 自己写的数据结构在 Documents/data_structure/python中
Documents/data_structure/cpp/LeetCode
sublime annaconda 3.6.5
pycharm /usr/local/Cellar/python3/3.6.1
linux机器
给出树的节点
class TreeNode:
def __init__(self,x):
self.val=x
self.left=None
self.right=None
题目
输入:
### A
# / \
# B C
# / \ / \
# D E F G
# / \
# H I
输出:
# LayerOrder:
# A
# BC
# DEFG
树的结构存储
python中如何存储树?
BFS 广度优先遍历
BFS用到的数据结构是quene
def bfs(self,root): # 传入树的根节点
quene=[]
q.append(root)
while len(q)!=0:
every_layer=[]
length=len(q)
for i in range(length):
r=q.pop(0)
if not r.left is None:
q.append(r.left)
if not r.right is None:
q.append(r.right)
every_layer.append(r.val)
print every_layer
print ' '
result.append(every_layer)