diff --git a/Area.java b/Area.java new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/first-contributions b/first-contributions new file mode 160000 index 000000000000..d58c69ca9ad0 --- /dev/null +++ b/first-contributions @@ -0,0 +1 @@ +Subproject commit d58c69ca9ad0641a155f3a6c95efb484dc0a5abe diff --git a/src/main/java/com/thealgorithms/datastructures/trees/InvertBinaryTree.java b/src/main/java/com/thealgorithms/datastructures/trees/InvertBinaryTree.java new file mode 100644 index 000000000000..a72a3418fbd9 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/trees/InvertBinaryTree.java @@ -0,0 +1,21 @@ +package com.thealgorithms.datastructures.trees; + +public final class InvertBinaryTree { + + private InvertBinaryTree() { + } + + public static BinaryTree.Node invertTree(BinaryTree.Node root) { + if (root == null) { + return null; + } + + BinaryTree.Node temp = root.left; + root.left = root.right; + root.right = temp; + + invertTree(root.left); + invertTree(root.right); + return root; + } +} diff --git a/src/main/java/com/thealgorithms/datastructures/trees/KthSmallestElementInBST.java b/src/main/java/com/thealgorithms/datastructures/trees/KthSmallestElementInBST.java new file mode 100644 index 000000000000..d7aba6660be1 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/trees/KthSmallestElementInBST.java @@ -0,0 +1,41 @@ +package com.thealgorithms.datastructures.trees; + +/** + * Finds the kth smallest element in a Binary Search Tree. + * + * Time Complexity: O(n) + * Space Complexity: O(h) + */ +public final class KthSmallestElementInBST { + + private KthSmallestElementInBST() { + } + + public static int kthSmallest(BinaryTree.Node root, int k) { + Counter counter = new Counter(); + BinaryTree.Node result = inorder(root, k, counter); + return result != null ? result.data : -1; + } + + private static BinaryTree.Node inorder(BinaryTree.Node node, int k, Counter counter) { + if (node == null) { + return null; + } + + BinaryTree.Node left = inorder(node.left, k, counter); + if (left != null) { + return left; + } + + counter.count++; + if (counter.count == k) { + return node; + } + + return inorder(node.right, k, counter); + } + + private static class Counter { + int count = 0; + } +} diff --git a/src/test/java/com/thealgorithms/datastructures/trees/InvertBinaryTreeTest.java b/src/test/java/com/thealgorithms/datastructures/trees/InvertBinaryTreeTest.java new file mode 100644 index 000000000000..255a819a0519 --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/trees/InvertBinaryTreeTest.java @@ -0,0 +1,42 @@ +package com.thealgorithms.datastructures.trees; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import org.junit.jupiter.api.Test; + +class InvertBinaryTreeTest { + + @Test + void testInvertTreeNormalCase() { + BinaryTree.Node root = new BinaryTree.Node(4); + root.left = new BinaryTree.Node(2); + root.right = new BinaryTree.Node(7); + root.left.left = new BinaryTree.Node(1); + root.left.right = new BinaryTree.Node(3); + + BinaryTree.Node inverted = InvertBinaryTree.invertTree(root); + + assertEquals(7, inverted.left.data); + assertEquals(2, inverted.right.data); + assertEquals(3, inverted.right.left.data); + assertEquals(1, inverted.right.right.data); + } + + @Test + void testInvertTreeSingleNode() { + BinaryTree.Node root = new BinaryTree.Node(1); + + BinaryTree.Node inverted = InvertBinaryTree.invertTree(root); + + assertEquals(1, inverted.data); + assertNull(inverted.left); + assertNull(inverted.right); + } + + @Test + void testInvertTreeNull() { + BinaryTree.Node inverted = InvertBinaryTree.invertTree(null); + + assertNull(inverted); + } +} diff --git a/src/test/java/com/thealgorithms/datastructures/trees/KthSmallestElementBSTTest.java b/src/test/java/com/thealgorithms/datastructures/trees/KthSmallestElementBSTTest.java new file mode 100644 index 000000000000..8ca9d07f737d --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/trees/KthSmallestElementBSTTest.java @@ -0,0 +1,17 @@ +package com.thealgorithms.datastructures.trees; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class KthSmallestElementBSTTest { + + @Test + void simpleTest() { + BinaryTree.Node root = new BinaryTree.Node(5); + root.left = new BinaryTree.Node(3); + root.right = new BinaryTree.Node(7); + + assertEquals(3, KthSmallestElementInBST.kthSmallest(root, 1)); + } +}