diff --git a/src/LowestCommonAncestorOfaBinarySearchTree235.java b/src/LowestCommonAncestorOfaBinarySearchTree235.java deleted file mode 100644 index b5fb394..0000000 --- a/src/LowestCommonAncestorOfaBinarySearchTree235.java +++ /dev/null @@ -1,33 +0,0 @@ -// Tree, Depth-First Search, Binary Search Tree, Binary Tree -public class LowestCommonAncestorOfaBinarySearchTree235 { - /* - Time Complexity: O(N), - where N is the number of nodes in the BST. - In the worst case we might be visiting all the nodes of the BST. - - Space Complexity: O(N). - This is because the maximum amount of space - utilized by the recursion stack would be N - since the height of a skewed BST could be N. - */ - public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { - if (root == p || root == q || (root.val > p.val && root.val < q.val) || (root.val < p.val && root.val > q.val)) { - return root; - } - if (root.val > p.val) { - return this.lowestCommonAncestor(root.left, p, q); - } - if (root.val < p.val) { - return this.lowestCommonAncestor(root.right, p, q); - } - return null; - } - - public static void main(String[] args) { - TreeNode p = new TreeNode(2, new TreeNode(0), new TreeNode(4, new TreeNode(3), new TreeNode(5))); - TreeNode q = new TreeNode(8, new TreeNode(7), new TreeNode(9)); - TreeNode root = new TreeNode(6, p, q); - System.out.println(new LowestCommonAncestorOfaBinarySearchTree235().lowestCommonAncestor(root, p, q).val); - // 6 - } -} diff --git a/src/main/java/com/leetcode/easy/LowestCommonAncestorOfABinarySearchTree235.java b/src/main/java/com/leetcode/easy/LowestCommonAncestorOfABinarySearchTree235.java new file mode 100644 index 0000000..e89a413 --- /dev/null +++ b/src/main/java/com/leetcode/easy/LowestCommonAncestorOfABinarySearchTree235.java @@ -0,0 +1,26 @@ +// Tags: Tree, DFS, Binary Search Tree +package com.leetcode.easy; + +import com.leetcode.datastructure.TreeNode; + +public class LowestCommonAncestorOfABinarySearchTree235 { + /* + Time complexity: O(N), + where N is the number of nodes in the BST. + In the worst case we might be visiting all the nodes of the BST. + + Space complexity: O(N). + This is because the maximum amount of space + utilized by the recursion stack would be N + since the height of a skewed BST could be N. + */ + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + if (p.val > root.val && q.val > root.val) { + return this.lowestCommonAncestor(root.right, p, q); + } + if (p.val < root.val && q.val < root.val) { + return this.lowestCommonAncestor(root.left, p, q); + } + return root; + } +} diff --git a/src/test/java/com/leetcode/easy/LowestCommonAncestorOfABinarySearchTree235Test.java b/src/test/java/com/leetcode/easy/LowestCommonAncestorOfABinarySearchTree235Test.java new file mode 100644 index 0000000..7beb201 --- /dev/null +++ b/src/test/java/com/leetcode/easy/LowestCommonAncestorOfABinarySearchTree235Test.java @@ -0,0 +1,47 @@ +package com.leetcode.easy; + +import com.leetcode.datastructure.TreeNode; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class LowestCommonAncestorOfABinarySearchTree235Test { + private LowestCommonAncestorOfABinarySearchTree235 solution; + + @BeforeEach + void setUp() { + solution = new LowestCommonAncestorOfABinarySearchTree235(); + } + + @Test + void testLowestCommonAncestor_Example1() { + TreeNode root = new TreeNode(6, + new TreeNode(2, new TreeNode(0), new TreeNode(4, new TreeNode(3), new TreeNode(5))), + new TreeNode(8, new TreeNode(7), new TreeNode(9))); + TreeNode p = root.left; // Node with value 2 + TreeNode q = root.right; // Node with value 8 + TreeNode result = solution.lowestCommonAncestor(root, p, q); + assertEquals(root, result); + } + + @Test + void testLowestCommonAncestor_Example2() { + TreeNode root = new TreeNode(6, + new TreeNode(2, new TreeNode(0), new TreeNode(4, new TreeNode(3), new TreeNode(5))), + new TreeNode(8, new TreeNode(7), new TreeNode(9))); + TreeNode p = root.left; // Node with value 2 + TreeNode q = root.left.right; // Node with value 4 + TreeNode result = solution.lowestCommonAncestor(root, p, q); + assertEquals(p, result); + } + + @Test + void testLowestCommonAncestor_Example3() { + TreeNode root = new TreeNode(2, new TreeNode(1), null); + TreeNode p = root; // Node with value 2 + TreeNode q = root.left; // Node with value 1 + TreeNode result = solution.lowestCommonAncestor(root, p, q); + assertEquals(root, result); + } +}