Skip to content

Commit 38e5090

Browse files
authored
Added task 235.
1 parent 6438952 commit 38e5090

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0201_0300.s0235_lowest_common_ancestor_of_a_binary_search_tree;
2+
3+
import com_github_leetcode.TreeNode;
4+
5+
public class Solution {
6+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
7+
while (true) {
8+
if (root.val > p.val && root.val > q.val) {
9+
root = root.left;
10+
} else if (root.val < p.val && root.val < q.val) {
11+
root = root.right;
12+
} else {
13+
break;
14+
}
15+
}
16+
return root;
17+
}
18+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
235\. Lowest Common Ancestor of a Binary Search Tree
2+
3+
Easy
4+
5+
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
6+
7+
According to the [definition of LCA on Wikipedia](https://en.wikipedia.org/wiki/Lowest_common_ancestor): “The lowest common ancestor is defined between two nodes `p` and `q` as the lowest node in `T` that has both `p` and `q` as descendants (where we allow **a node to be a descendant of itself**).”
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2018/12/14/binarysearchtree_improved.png)
12+
13+
**Input:** root = \[6,2,8,0,4,7,9,null,null,3,5\], p = 2, q = 8
14+
15+
**Output:** 6
16+
17+
**Explanation:** The LCA of nodes 2 and 8 is 6.
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2018/12/14/binarysearchtree_improved.png)
22+
23+
**Input:** root = \[6,2,8,0,4,7,9,null,null,3,5\], p = 2, q = 4
24+
25+
**Output:** 2
26+
27+
**Explanation:** The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
28+
29+
**Example 3:**
30+
31+
**Input:** root = \[2,1\], p = 2, q = 1
32+
33+
**Output:** 2
34+
35+
**Constraints:**
36+
37+
* The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.
38+
* <code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code>
39+
* All `Node.val` are **unique**.
40+
* `p != q`
41+
* `p` and `q` will exist in the BST.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g0201_0300.s0235_lowest_common_ancestor_of_a_binary_search_tree;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.TreeNode;
7+
import org.junit.Test;
8+
9+
public class SolutionTest {
10+
@Test
11+
public void lowestCommonAncestor() {
12+
TreeNode leftNodeLeftNode = new TreeNode(0);
13+
TreeNode leftNodeRightNode = new TreeNode(4, new TreeNode(3), new TreeNode(5));
14+
TreeNode leftNode = new TreeNode(2, leftNodeLeftNode, leftNodeRightNode);
15+
TreeNode rightNode = new TreeNode(6, new TreeNode(7), new TreeNode(9));
16+
TreeNode root = new TreeNode(6, leftNode, rightNode);
17+
assertThat(
18+
new Solution().lowestCommonAncestor(root, new TreeNode(2), new TreeNode(8)).val,
19+
equalTo(6));
20+
}
21+
}

0 commit comments

Comments
 (0)