宽度优先遍历——python
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time: 2018-05-25 10:16# @Author: xxd# @File: test.pyclass Node:def __init__(self,data,left=None,right=None):self.data=data...
·
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018-05-25 10:16
# @Author : xxd
# @File : test.py
class Node:
def __init__(self,data,left=None,right=None):
self.data=data
self.left=left
self.right=right
def BFS(Node):
if Node == None:
return
quene = []
quene.append(Node)
while quene:
Node = quene.pop(0)
print(Node.data)
if Node.left:
quene.append(Node.left)
if Node.right:
quene.append(Node.right)
if __name__ == '__main__':
t=Node(1,Node(2),Node(3))
BFS(t)
更多推荐
已为社区贡献2条内容
所有评论(0)