Add inorder binary tree traversal (#3898)

This commit is contained in:
Albina Gimaletdinova
2023-02-25 23:58:06 +03:00
committed by GitHub
parent 6d13d95e41
commit 45923d6872
3 changed files with 121 additions and 4 deletions

View File

@ -0,0 +1,60 @@
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 class 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);
}
}