Skip to content

Commit 3499c1b

Browse files
authored
Add postorder binary tree traversal (#3899)
1 parent b98dc2c commit 3499c1b

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.thealgorithms.datastructures.trees;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Given tree is traversed in a 'post-order' way: LEFT -> RIGHT -> ROOT.
7+
* Below are given the recursive and iterative implementations.
8+
* <p>
9+
* Complexities:
10+
* Recursive: O(n) - time, O(n) - space, where 'n' is the number of nodes in a tree.
11+
* <p>
12+
* Iterative: O(n) - time, O(h) - space, where 'n' is the number of nodes in a tree
13+
* and 'h' is the height of a binary tree.
14+
* In the worst case 'h' can be O(n) if tree is completely unbalanced, for instance:
15+
* 5
16+
* \
17+
* 6
18+
* \
19+
* 7
20+
* \
21+
* 8
22+
*
23+
* @author Albina Gimaletdinova on 21/02/2023
24+
*/
25+
public class PostOrderTraversal {
26+
public static List<Integer> recursivePostOrder(BinaryTree.Node root) {
27+
List<Integer> result = new ArrayList<>();
28+
recursivePostOrder(root, result);
29+
return result;
30+
}
31+
32+
public static List<Integer> iterativePostOrder(BinaryTree.Node root) {
33+
LinkedList<Integer> result = new LinkedList<>();
34+
if (root == null) {
35+
return result;
36+
}
37+
38+
Deque<BinaryTree.Node> stack = new ArrayDeque<>();
39+
stack.push(root);
40+
while (!stack.isEmpty()) {
41+
BinaryTree.Node node = stack.pop();
42+
result.addFirst(node.data);
43+
if (node.left != null) {
44+
stack.push(node.left);
45+
}
46+
if (node.right != null) {
47+
stack.push(node.right);
48+
}
49+
}
50+
51+
return result;
52+
}
53+
54+
private static void recursivePostOrder(BinaryTree.Node root, List<Integer> result) {
55+
if (root == null) {
56+
return;
57+
}
58+
recursivePostOrder(root.left, result);
59+
recursivePostOrder(root.right, result);
60+
result.add(root.data);
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.thealgorithms.datastructures.trees;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
/**
11+
* Given tree is traversed in a 'post-order' way: LEFT -> RIGHT -> ROOT.
12+
*
13+
* @author Albina Gimaletdinova on 21/02/2023
14+
*/
15+
public class PostOrderTraversalTest {
16+
@Test
17+
public void testNullRoot() {
18+
assertEquals(Collections.emptyList(), PostOrderTraversal.recursivePostOrder(null));
19+
assertEquals(Collections.emptyList(), PostOrderTraversal.iterativePostOrder(null));
20+
}
21+
22+
/*
23+
1
24+
/ \
25+
2 3
26+
/\ /\
27+
4 5 6 7
28+
*/
29+
@Test
30+
public void testPostOrder() {
31+
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
32+
List<Integer> expected = List.of(4, 5, 2, 6, 7, 3, 1);
33+
34+
assertEquals(expected, PostOrderTraversal.recursivePostOrder(root));
35+
assertEquals(expected, PostOrderTraversal.iterativePostOrder(root));
36+
}
37+
38+
/*
39+
5
40+
\
41+
6
42+
\
43+
7
44+
\
45+
8
46+
*/
47+
@Test
48+
public void testPostOrderNonBalanced() {
49+
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{5, null, 6, null, 7, null, 8});
50+
List<Integer> expected = List.of(8, 7, 6, 5);
51+
52+
assertEquals(expected, PostOrderTraversal.recursivePostOrder(root));
53+
assertEquals(expected, PostOrderTraversal.iterativePostOrder(root));
54+
}
55+
}

0 commit comments

Comments
 (0)