Skip to content

Commit e0e9003

Browse files
add 1644
1 parent 00c8d38 commit e0e9003

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ _If you like this project, please leave me a star._ ★
130130
|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|
131131
|1652|[Defuse the Bomb](https://leetcode.com/problems/defuse-the-bomb/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1652.java) ||Easy|Array|
132132
|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|
133+
|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|
133134
|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|
134135
|1641|[Count Sorted Vowel Strings](https://leetcode.com/problems/count-sorted-vowel-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1641.java) |[:tv:](https://youtu.be/gdH4yfgfwiU)|Medium|Math, DP, Backtracking|
135136
|1640|[Check Array Formation Through Concatenation](https://leetcode.com/problems/check-array-formation-through-concatenation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1640.java) ||Easy|Array, Sort|
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
public class _1644 {
6+
public static class Solution1 {
7+
/**
8+
* This is my not so elegant but original solution to get it accepted.
9+
*/
10+
boolean[] exists = new boolean[2];
11+
12+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
13+
exists(p, root, 0);
14+
exists(q, root, 1);
15+
if (!exists[0] || !exists[1]) {
16+
return null;
17+
}
18+
return dfs(root, p, q);
19+
}
20+
21+
private void exists(TreeNode target, TreeNode root, int index) {
22+
if (root == null) {
23+
return;
24+
}
25+
if (target == root) {
26+
exists[index] = true;
27+
return;
28+
}
29+
if (!exists[index]) {
30+
exists(target, root.left, index);
31+
}
32+
if (!exists[index]) {
33+
exists(target, root.right, index);
34+
}
35+
}
36+
37+
private TreeNode dfs(TreeNode root, TreeNode p, TreeNode q) {
38+
if (root == null || p == root || q == root) {
39+
return root;
40+
}
41+
TreeNode left = lowestCommonAncestor(root.left, p, q);
42+
TreeNode right = lowestCommonAncestor(root.right, p, q);
43+
if (left != null && right != null) {
44+
return root;
45+
}
46+
return left != null ? left : right;
47+
}
48+
}
49+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._1644;
6+
import org.junit.Test;
7+
8+
import java.util.Arrays;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _1644Test {
13+
private static _1644.Solution1 solution1;
14+
15+
@Test
16+
public void test1() {
17+
solution1 = new _1644.Solution1();
18+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(3, 5, 1, 6, 2, 0, 8, null, null, 7, 4));
19+
TreeUtils.printBinaryTree(root);
20+
TreeNode p = TreeUtils.constructBinaryTree(Arrays.asList(5, 6, 2, null, null, 7, 4));
21+
TreeUtils.printBinaryTree(p);
22+
TreeNode q = new TreeNode(10);
23+
TreeNode actual = solution1.lowestCommonAncestor(root, p, q);
24+
System.out.println("actual: " + actual);
25+
assertEquals(null, actual);
26+
}
27+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy