Skip to content

Commit 2110c6b

Browse files
update 235
1 parent 24b7a6a commit 2110c6b

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/main/java/com/fishercoder/solutions/_235.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
99
if (root == null || p == root || q == root) {
1010
return root;
1111
}
12-
if ((root.val - p.val) * (root.val - q.val) > 0) {
13-
if (root.val - p.val > 0) {
14-
return lowestCommonAncestor(root.left, p, q);
15-
}
12+
if (root.val > p.val && root.val > q.val) {
13+
return lowestCommonAncestor(root.left, p, q);
14+
} else if (root.val < p.val && root.val < q.val) {
1615
return lowestCommonAncestor(root.right, p, q);
1716
}
1817
return root;

src/test/java/com/fishercoder/_235Test.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,18 @@ public void test2() {
4949

5050
assertEquals(p, solution1.lowestCommonAncestor(root, p, q));
5151
}
52+
53+
@Test
54+
public void test3() {
55+
root = TreeUtils.constructBinaryTree(Arrays.asList(0, -1000000000, 1000000000));
56+
TreeUtils.printBinaryTree(root);
57+
58+
p = TreeUtils.constructBinaryTree(Arrays.asList(-1000000000));
59+
TreeUtils.printBinaryTree(p);
60+
61+
q = TreeUtils.constructBinaryTree(Arrays.asList(1000000000));
62+
TreeUtils.printBinaryTree(q);
63+
64+
assertEquals(root, solution1.lowestCommonAncestor(root, p, q));
65+
}
5266
}

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