Skip to content

Commit 33e668e

Browse files
add 1650
1 parent ef94e1b commit 33e668e

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ _If you like this project, please leave me a star._ ★
138138
|1657|[Determine if Two Strings Are Close](https://leetcode.com/problems/determine-if-two-strings-are-close/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1657.java) |[:tv:](https://youtu.be/-jXQK-UeChU)|Medium|Greedy|
139139
|1656|[Design an Ordered Stream](https://leetcode.com/problems/design-an-ordered-stream/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1656.java) ||Easy|Array, Design|
140140
|1652|[Defuse the Bomb](https://leetcode.com/problems/defuse-the-bomb/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1652.java) ||Easy|Array|
141+
|1650|[Lowest Common Ancestor of a Binary Tree III](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1650.java) ||Medium|HashTable, Binary Tree, Tree|
141142
|1646|[Get Maximum in Generated Array](https://leetcode.com/problems/get-maximum-in-generated-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1646.java) ||Easy|Array|
142143
|1644|[Lowest Common Ancestor of a Binary Tree II](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1644.java) ||Medium|Binary Tree, DFS|
143144
|1642|[Furthest Building You Can Reach](https://leetcode.com/problems/furthest-building-you-can-reach/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1642.java) ||Medium|Binary Search, Heap|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class _1650 {
7+
class Node {
8+
public int val;
9+
public Node parent;
10+
public Node left;
11+
public Node right;
12+
13+
public Node(int val) {
14+
this.val = val;
15+
}
16+
}
17+
18+
public static class Solution1 {
19+
public Node lowestCommonAncestor(Node p, Node q) {
20+
Node a = p, b = q;
21+
while (a != b) {
22+
a = a == null ? p : a.parent;
23+
b = b == null ? q : b.parent;
24+
}
25+
return a;
26+
}
27+
}
28+
29+
public static class Solution2 {
30+
public Node lowestCommonAncestor(Node p, Node q) {
31+
Set<Node> set = new HashSet<>();
32+
while (p != null) {
33+
set.add(p);
34+
p = p.parent;
35+
}
36+
while (q != null) {
37+
if (set.contains(q)) {
38+
return q;
39+
}
40+
q = q.parent;
41+
}
42+
return null;
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)