Skip to content

Commit 596ffea

Browse files
authored
Merge pull request #27 from kobukuro/feat/lowest-common-ancestor-of-a-binary-tree
Add solution for the "Lowest Common Ancestor of a Binary Tree" problem with unit tests.
2 parents 25c5bc5 + e497ae3 commit 596ffea

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Tags: Tree, DFS
2+
package com.leetcode.medium;
3+
4+
import com.leetcode.datastructure.TreeNode;
5+
6+
public class LowestCommonAncestorOfABinaryTree236 {
7+
/*
8+
Time complexity: O(N),
9+
where N is the number of nodes in the binary tree.
10+
In the worst case we might be visiting all the nodes of the binary tree.
11+
12+
Space complexity: O(N).
13+
This is because the maximum amount of space utilized by the recursion stack would be N
14+
since the height of a skewed binary tree could be N.
15+
*/
16+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
17+
if (root == p || root == q || root == null) {
18+
return root;
19+
}
20+
TreeNode left = lowestCommonAncestor(root.left, p, q);
21+
TreeNode right = lowestCommonAncestor(root.right, p, q);
22+
if (left != null && right != null) {
23+
return root;
24+
}
25+
if (left != null) {
26+
return left;
27+
}
28+
return right;
29+
}
30+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.leetcode.medium;
2+
3+
import com.leetcode.datastructure.TreeNode;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class LowestCommonAncestorOfABinaryTree236Test {
10+
private LowestCommonAncestorOfABinaryTree236 solution;
11+
12+
@BeforeEach
13+
void setUp() {
14+
solution = new LowestCommonAncestorOfABinaryTree236();
15+
}
16+
17+
@Test
18+
void testLowestCommonAncestor_Example1() {
19+
TreeNode root = new TreeNode(3,
20+
new TreeNode(5, new TreeNode(6), new TreeNode(2, new TreeNode(7), new TreeNode(4))),
21+
new TreeNode(1, new TreeNode(0), new TreeNode(8)));
22+
TreeNode p = root.left; // Node with value 5
23+
TreeNode q = root.right; // Node with value 1
24+
TreeNode result = solution.lowestCommonAncestor(root, p, q);
25+
assertEquals(root, result);
26+
}
27+
28+
@Test
29+
void testLowestCommonAncestor_Example2() {
30+
TreeNode root = new TreeNode(3,
31+
new TreeNode(5, new TreeNode(6), new TreeNode(2, new TreeNode(7), new TreeNode(4))),
32+
new TreeNode(1, new TreeNode(0), new TreeNode(8)));
33+
TreeNode p = root.left; // Node with value 5
34+
TreeNode q = root.left.right.right; // Node with value 4
35+
TreeNode result = solution.lowestCommonAncestor(root, p, q);
36+
assertEquals(p, result);
37+
}
38+
39+
@Test
40+
void testLowestCommonAncestor_Example3() {
41+
TreeNode root = new TreeNode(1, new TreeNode(2), null);
42+
TreeNode p = root; // Node with value 1
43+
TreeNode q = root.left; // Node with value 2
44+
TreeNode result = solution.lowestCommonAncestor(root, p, q);
45+
assertEquals(p, result);
46+
}
47+
}

0 commit comments

Comments
 (0)