from Tree import Node import argparse """ Postorder (Left, Right, Root) : 4 5 2 3 1 """ def postorder_recursive(root): if not root: return postorder_recursive(root.left) postorder_recursive(root.right) print(root.val) root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) postorder_recursive(root)
currentCounter -= 1 print(item.val, end=" ") if item.left: q.append(item.left) nextCount += 1 if item.right: q.append(item.right) nextCount += 1 if currentCounter == 0: print() currentCounter = nextCount nextCount = 0 if __name__ == "__main__": tree = Node(1) t2 = Node(2) t2.left = Node(4) t3 = Node(3) t3.left = Node(5) t3.right = Node(6) tree.left = t2 tree.right = t3 levelOrderPrint(tree) #result should be: #1 #2 3 #4 5 6