Repository: TheAlgorithms/Java
Pull request #3899 (2023-02-21 20:02:16)

Added recursive&iterative postorder binary tree traversal
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.



Patch:
diff --git a/src/main/java/com/thealgorithms/datastructures/trees/PostOrderTraversal.java b/src/main/java/com/thealgorithms/datastructures/trees/PostOrderTraversal.java
new file mode 100644
index 00000000..f7afb60a
--- /dev/null
+++ b/src/main/java/com/thealgorithms/datastructures/trees/PostOrderTraversal.java
@@ -0,0 +1,62 @@
+package com.thealgorithms.datastructures.trees;
+
+import java.util.*;
+
+/**
+ * Given tree is traversed in a 'post-order' way: LEFT -> RIGHT -> ROOT.
+ * Below are given the recursive and iterative implementations.
+ * <p>
+ * Complexities:
+ * Recursive: O(n) - time, O(n) - space, where 'n' is the number of nodes in a tree.
+ * <p>
+ * Iterative: O(n) - time, O(h) - space, where 'n' is the number of nodes in a tree
+ * and 'h' is the height of a binary tree.
+ * In the worst case 'h' can be O(n) if tree is completely unbalanced, for instance:
+ * 5
+ *  \
+ *   6
+ *    \
+ *     7
+ *      \
+ *       8
+ *
+ * @author Albina Gimaletdinova on 21/02/2023
+ */
+public class PostOrderTraversal {
+    public static List<Integer> recursivePostOrder(BinaryTree.Node root) {
+        List<Integer> result = new ArrayList<>();
+        recursivePostOrder(root, result);
+        return result;
+    }
+
+    public static List<Integer> iterativePostOrder(BinaryTree.Node root) {
+        LinkedList<Integer> result = new LinkedList<>();
+        if (root == null) {
+            return result;
+        }
+
+        Deque<BinaryTree.Node> stack = new ArrayDeque<>();
+        stack.push(root);
+        while (!stack.isEmpty()) {
+            BinaryTree.Node node = stack.pop();
+            result.addFirst(node.data);
+            if (node.left != null) {
+                stack.push(node.left);
+            }
+            if (node.right != null) {
+                stack.push(node.right);
+            }
+        }
+
+        return result;
+    }
+
+    private static void recursivePostOrder(BinaryTree.Node root, List<Integer> result) {
+        if (root == null) {
+            return;
+        }
+        recursivePostOrder(root.left, result);
+        recursivePostOrder(root.right, result);
+        result.add(root.data);
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/datastructures/trees/PostOrderTraversalTest.java b/src/test/java/com/thealgorithms/datastructures/trees/PostOrderTraversalTest.java
new file mode 100644
index 00000000..2774dc09
--- /dev/null
+++ b/src/test/java/com/thealgorithms/datastructures/trees/PostOrderTraversalTest.java
@@ -0,0 +1,55 @@
+package com.thealgorithms.datastructures.trees;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Given tree is traversed in a 'post-order' way: LEFT -> RIGHT -> ROOT.
+ *
+ * @author Albina Gimaletdinova on 21/02/2023
+ */
+public class PostOrderTraversalTest {
+    @Test
+    public void testNullRoot() {
+        assertEquals(Collections.emptyList(), PostOrderTraversal.recursivePostOrder(null));
+        assertEquals(Collections.emptyList(), PostOrderTraversal.iterativePostOrder(null));
+    }
+
+    /*
+         1
+        / \
+       2   3
+      /\   /\
+     4  5 6  7
+    */
+    @Test
+    public void testPostOrder() {
+        final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
+        List<Integer> expected = List.of(4, 5, 2, 6, 7, 3, 1);
+
+        assertEquals(expected, PostOrderTraversal.recursivePostOrder(root));
+        assertEquals(expected, PostOrderTraversal.iterativePostOrder(root));
+    }
+
+    /*
+         5
+          \
+           6
+            \
+             7
+              \
+               8
+    */
+    @Test
+    public void testPostOrderNonBalanced() {
+        final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{5, null, 6, null, 7, null, 8});
+        List<Integer> expected = List.of(8, 7, 6, 5);
+
+        assertEquals(expected, PostOrderTraversal.recursivePostOrder(root));
+        assertEquals(expected, PostOrderTraversal.iterativePostOrder(root));
+    }
+}