/
NikolayIvkin
/
TheAlgorithms
Обзор
Документация
Войти
/
NikolayIvkin
/
TheAlgorithms
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/java/com/thealgorithms/datastructures/trees/PreOrderTraversal.java
67 строк
2 KB
Samuel Facchinello
style: enable `NeedBraces` in checkstyle (#5227)
13 июн 2024, 22:00
Не верифицирован
13 июн 2024, 22:00
87b17e0
Код
Авторство
О чём код?
package com.thealgorithms.datastructures.trees; import java.util.ArrayList; import java.util.Deque; import java.util.LinkedList; import java.util.List; /** * Given tree is traversed in a 'pre-order' way: ROOT -> LEFT -> 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 17/02/2023 */ public final class PreOrderTraversal { private PreOrderTraversal() { } public static List<Integer> recursivePreOrder(BinaryTree.Node root) { List<Integer> result = new ArrayList<>(); recursivePreOrder(root, result); return result; } public static List<Integer> iterativePreOrder(BinaryTree.Node root) { List<Integer> result = new ArrayList<>(); if (root == null) { return result; } Deque<BinaryTree.Node> stack = new LinkedList<>(); stack.push(root); while (!stack.isEmpty()) { BinaryTree.Node node = stack.pop(); result.add(node.data); if (node.right != null) { stack.push(node.right); } if (node.left != null) { stack.push(node.left); } } return result; } private static void recursivePreOrder(BinaryTree.Node root, List<Integer> result) { if (root == null) { return; } result.add(root.data); recursivePreOrder(root.left, result); recursivePreOrder(root.right, result); } }