剑指offer-顺序打印二叉树节点(系列)-树-python
转载自 https://blog.csdn.net/u010005281/article/details/79761056 非常感谢!
首先创建二叉树,然后按各种方式打印:
class treeNode: def __init__(self, x): self.left = None self.right = None self.val = x class Solution: # 给定二叉树的前序遍历和中序遍历,获得该二叉树 def getBSTwithPreTin(self, pre, tin): if not pre or not tin: return None root = treeNode(pre[0]) for order, item in enumerate(tin): if root.val == item: root.left = self.getBSTwithPreTin(pre[1:order + 1], tin[:order]) root.right = self.getBSTwithPreTin(pre[order + 1:], tin[order + 1:]) return root #从上往下打印出二叉树的每个节点,同层节点从左至右打印 #使用队列先进先出来依次打印 def PrintFromTopToBottom(self, root): array = [] result = [] if root == None: return result #root为根,其值为根节点的值 array.append(root) while array: newNode = array.pop(0) result.append(newNode.val) if newNode.left != None: array.append(newNode.left)#先放左边 if newNode.right != None: array.append(newNode.right) return result #从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。 def Print(self, pRoot): if not pRoot: return [] resultList = [] curLayer = [pRoot] count = 0 while curLayer: curList = [] nextLayer = [] for node in curLayer: curList.append(node.val) if node.left: nextLayer.append(node.left) if node.right: nextLayer.append(node.right) if count // 2 == 0: curList.reverse() resultList.append(curList) curLayer = nextLayer return resultList #请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印, #第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。 #方式1: #1. 按序获取每一层节点的值; # 2. 将偶数层节点的值倒序。 def PrintZ_1(self, pRoot): # write code here if pRoot == None: return [] cur_layer = [pRoot] res = [] isEvenLayer = True while cur_layer: curlist = [] nextlayer = [] isEvenLayer = not isEvenLayer #是否倒叙 for node in cur_layer: curlist.append(node.val) if node.left: nextlayer.append(node.left) if node.right: nextlayer.append(node.right) if isEvenLayer == False: res.append(curlist) else: res.append(curlist[::-1]) cur_layer = nextlayer return res #方式2: # 获取每一层的节点的值时,如果是偶数层,则将每个节点的值插入到列表的头部, # 即实现了获取节点值时如果是偶数层则倒序排列的效果: def PrintZ_2(self, pRoot): # write code here if pRoot == None: return [] cur_layer = [pRoot] res = [] isEvenLayer = True while cur_layer: curlist = [] nextlayer = [] isEvenLayer = not isEvenLayer # 是否倒叙 for node in cur_layer: if isEvenLayer == False: curlist.append(node.val) else: curlist.insert(0,node.val) if node.left: nextlayer.append(node.left) if node.right: nextlayer.append(node.right) res.append(curlist) cur_layer = nextlayer return res if __name__ == '__main__': # flag = "printTreeNode" # flag = "printTreeNode_line" # flag = "printTreeNode_Z1" flag = "printTreeNode_Z2" solution = Solution() preorder_seq = [1, 2, 4, 7, 3, 5, 6, 8] middleorder_seq = [4, 7, 2, 1, 5, 3, 8, 6] treeRoot1 = solution.getBSTwithPreTin(preorder_seq, middleorder_seq) if flag == "printTreeNode": newArray = solution.PrintFromTopToBottom(treeRoot1) print(newArray) elif flag == "printTreeNode_line": newArray = solution.Print(treeRoot1) print(newArray) elif flag == "printTreeNode_Z1": newArray = solution.PrintZ_1(treeRoot1) print(newArray) else: newArray = solution.PrintZ_2(treeRoot1) print(newArray)