import Queue class TreeNode(): def __init__(self,value,leftNode=None,rightNode=None): self.value = value self.leftNode = leftNode self.rightNode = rightNode 1个问句 TreeView.py,用的是 python2.7 class Tree(): def leftViews(self,root): q = Queue.Queue() q.put(root) while(not q.empty()): size = q.qsize() for i in range(size): tempNode = q.get() if i==0: print tempNode.value else: pass if tempNode.leftNode!=None: q.put(tempNode.leftNode) if tempNode.rightNode!=None: q.put(tempNode.rightNode) if __name__ == '__main__': tree = Tree() node6 = TreeNode(30) node5 = TreeNode(25,None,node6) node4 = TreeNode(15) node3 = TreeNode(8) node2 = TreeNode(20,node4,node5) node1 = TreeNode(6,None,node3) root = TreeNode(10,node1,node2) tree.leftViews(root)
二叉树的左视图-Python
最新推荐文章于 2023-03-17 15:57:46 发布