Traversal { public List<Integer> postOrderTraverse(BinaryTreeNode root) { List<Integer> result = new ArrayList<>(); if (root != null) { Stack<BinaryTreeNode> stack = new Stack<>(); BinaryTreeNode prev = null; stack.push(root); while (!stack.isEmpty()){ BinaryTreeNode cur = stack.peek(); if (prev == null || prev.left == cur || prev.right == cur) { if (cur.left != null) { stack.push(cur.left); } else if (cur.right != null) { stack.push(cur.right); } else { result.add(cur.val); stack.pop(); } } else if (cur.left == prev) { if (cur.right != null) { stack.push(cur.right); } else { stack.pop(); result.add(cur.val); } } else { stack.pop(); result.add(cur.val); } prev = cur; } } return result; }  List<Integer> preOrderTraverse(BinaryTreeNode root); List<Integer> inOrderTraverse(BinaryTreeNode root); List<Integer> postOrderTraverse(BinaryTreeNode root);  }
@Test(dataProvider = "postOrderTree") public void testPostOrder(BinaryTreeNode head, List<Integer> expectedResult){ Traversal traversal = new Traversal(); assertEquals(traversal.postOrderTraverse(head), expectedResult); }