Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite0e9003

Browse files
add 1644
1 parent00c8d38 commite0e9003

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+
packagecom.fishercoder.solutions;
2+
3+
importcom.fishercoder.common.classes.TreeNode;
4+
5+
publicclass_1644 {
6+
publicstaticclassSolution1 {
7+
/**
8+
* This is my not so elegant but original solution to get it accepted.
9+
*/
10+
boolean[]exists =newboolean[2];
11+
12+
publicTreeNodelowestCommonAncestor(TreeNoderoot,TreeNodep,TreeNodeq) {
13+
exists(p,root,0);
14+
exists(q,root,1);
15+
if (!exists[0] || !exists[1]) {
16+
returnnull;
17+
}
18+
returndfs(root,p,q);
19+
}
20+
21+
privatevoidexists(TreeNodetarget,TreeNoderoot,intindex) {
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+
privateTreeNodedfs(TreeNoderoot,TreeNodep,TreeNodeq) {
38+
if (root ==null ||p ==root ||q ==root) {
39+
returnroot;
40+
}
41+
TreeNodeleft =lowestCommonAncestor(root.left,p,q);
42+
TreeNoderight =lowestCommonAncestor(root.right,p,q);
43+
if (left !=null &&right !=null) {
44+
returnroot;
45+
}
46+
returnleft !=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+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.classes.TreeNode;
4+
importcom.fishercoder.common.utils.TreeUtils;
5+
importcom.fishercoder.solutions._1644;
6+
importorg.junit.Test;
7+
8+
importjava.util.Arrays;
9+
10+
importstaticorg.junit.Assert.assertEquals;
11+
12+
publicclass_1644Test {
13+
privatestatic_1644.Solution1solution1;
14+
15+
@Test
16+
publicvoidtest1() {
17+
solution1 =new_1644.Solution1();
18+
TreeNoderoot =TreeUtils.constructBinaryTree(Arrays.asList(3,5,1,6,2,0,8,null,null,7,4));
19+
TreeUtils.printBinaryTree(root);
20+
TreeNodep =TreeUtils.constructBinaryTree(Arrays.asList(5,6,2,null,null,7,4));
21+
TreeUtils.printBinaryTree(p);
22+
TreeNodeq =newTreeNode(10);
23+
TreeNodeactual =solution1.lowestCommonAncestor(root,p,q);
24+
System.out.println("actual: " +actual);
25+
assertEquals(null,actual);
26+
}
27+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp