Skip to content

Commit ac555a7

Browse files
add 235
1 parent 6d691b7 commit ac555a7

File tree

3 files changed

+29
-47
lines changed

3 files changed

+29
-47
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@ Your ideas/fixes/algorithms are more than welcome!
244244
|239|[Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/)|[Solution](../master/src/main/java/com/stevesun/solutions/SlidingWindowMaximum.java)| O(nlogn)|O(k) | Hard| Heap
245245
|238|[Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/)|[Solution](../master/src/main/java/com/stevesun/solutions/ProductofArrayExceptSelf.java)| O(n)|O(1) | Medium| Array
246246
|237|[Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/)|[Solution](../master/src/main/java/com/stevesun/solutions/_237.java)| O(1)|O(1) | Easy| LinkedList
247-
|235|[Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/)|[Solution](../master/src/main/java/com/stevesun/solutions/LowestCommonAncestorOfABinaryTree.java)| O(h)|O(1) | Medium| DFS
248-
|235|[Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)|[Solution](../master/src/main/java/com/stevesun/solutions/LowestCommonAncestorOfABinarySearchTree.java)| O(h)|O(1) | Easy| DFS
247+
|236|[Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/)|[Solution](../master/src/main/java/com/stevesun/solutions/LowestCommonAncestorOfABinaryTree.java)| O(h)|O(1) | Medium| DFS
248+
|235|[Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)|[Solution](../master/src/main/java/com/stevesun/solutions/_235.java)| O(h)|O(1) | Easy| DFS
249249
|233|[Number of Digit One](https://leetcode.com/problems/number-of-digit-one/)|[Solution](../master/src/main/java/com/stevesun/solutions/NumberofDigitOne.java)| O(n)|O(1) | Hard| Math
250250
|229|[Majority Element II](https://leetcode.com/problems/majority-element-ii/)|[Solution](../master/src/main/java/com/stevesun/solutions/MajorityElementII.java)| O(n)|O(n) | Medium|
251251
|228|[Summary Ranges](https://leetcode.com/problems/summary-ranges/)|[Solution](../master/src/main/java/com/stevesun/solutions/SummaryRanges.java)| O(n)|O(1) | Medium| Array

src/main/java/com/stevesun/solutions/LowestCommonAncestorOfABinarySearchTree.java

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.stevesun.solutions;
2+
3+
import com.stevesun.common.classes.TreeNode;
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: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
8+
9+
_______6______
10+
/ \
11+
___2__ ___8__
12+
/ \ / \
13+
0 _4 7 9
14+
/ \
15+
3 5
16+
For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
17+
18+
*/
19+
public class _235 {
20+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
21+
if (root == null || p == root || q == root) return root;
22+
if ((root.val - p.val) * (root.val - q.val) <= 0) return root;
23+
if ((root.val - p.val) * (root.val - q.val) > 0 && (root.val - p.val) > 0) return lowestCommonAncestor(root.left, p, q);
24+
if ((root.val - p.val) * (root.val - q.val) > 0 && (root.val - p.val) < 0) return lowestCommonAncestor(root.right, p, q);
25+
return root;
26+
}
27+
}

0 commit comments

Comments
 (0)