def test_postorder(self): n1 = Node(100) n2 = Node(50) n3 = Node(150) n4 = Node(25) n5 = Node(75) n6 = Node(125) n7 = Node(175) n1.left, n1.right = n2, n3 n2.left, n2.right = n4, n5 n3.left, n3.right = n6, n7 self.assertEqual([25, 75, 50, 125, 175, 150, 100], postorder(n1)) self.assertEqual([25, 75, 50, 125, 175, 150, 100], postorder_rec(n1))
def test_postorder(self): tree = create_tree() self.assertEqual([25, 75, 50, 125, 175, 150, 100], postorder(tree)) self.assertEqual([25, 75, 50, 125, 175, 150, 100], postorder_rec(tree))
from algorithms.tree.traversal import postorder class TreeNode: def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right n1 = TreeNode(6) n2 = TreeNode(2) n3 = TreeNode(8) n4 = TreeNode(0) n5 = TreeNode(4) n6 = TreeNode(3) n7 = TreeNode(5) n8 = TreeNode(7) n9 = TreeNode(9) n1.left, n1.right = n2, n3 n2.left, n2.right = n4, n5 n5.left, n5.right = n6, n7 n3.left, n3.right = n8, n9 print(postorder(n1))