forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInorderTraversal.java
More file actions
64 lines (59 loc) · 1.78 KB
/
InorderTraversal.java
File metadata and controls
64 lines (59 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.thealgorithms.datastructures.trees;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
/**
* Given tree is traversed in an 'inorder' way: LEFT -> ROOT -> RIGHT.
* Below are given the recursive and iterative implementations.
*
* Complexities:
* Recursive: O(n) - time, O(n) - space, where 'n' is the number of nodes in a tree.
*
* 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 final class InorderTraversal {
private InorderTraversal() {
}
public static List<Integer> recursiveInorder(BinaryTree.Node root) {
List<Integer> result = new ArrayList<>();
recursiveInorder(root, result);
return result;
}
public static List<Integer> iterativeInorder(BinaryTree.Node root) {
List<Integer> result = new ArrayList<>();
if (root == null) {
return result;
}
Deque<BinaryTree.Node> stack = new ArrayDeque<>();
while (!stack.isEmpty() || root != null) {
while (root != null) {
stack.push(root);
root = root.left;
}
root = stack.pop();
result.add(root.data);
root = root.right;
}
return result;
}
private static void recursiveInorder(BinaryTree.Node root, List<Integer> result) {
if (root == null) {
return;
}
recursiveInorder(root.left, result);
result.add(root.data);
recursiveInorder(root.right, result);
}
}