Skip to content

Commit 236a8f4

Browse files
committed
二刷235
1 parent 2871a17 commit 236a8f4

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

docs/0235-lowest-common-ancestor-of-a-binary-search-tree.adoc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,26 @@ image::images/0235-06.png[{image_attr}]
5858

5959

6060
[[src-0235]]
61+
[tabs]
62+
====
63+
一刷::
64+
+
65+
--
6166
[{java_src_attr}]
6267
----
6368
include::{sourcedir}/_0235_LowestCommonAncestorOfABinarySearchTree.java[tag=answer]
6469
----
70+
--
71+
72+
二刷::
73+
+
74+
--
75+
[{java_src_attr}]
76+
----
77+
include::{sourcedir}/_0235_LowestCommonAncestorOfABinarySearchTree_2.java[tag=answer]
78+
----
79+
--
80+
====
6581

6682
作为这道题的延伸,继续看 xref:0236-lowest-common-ancestor-of-a-binary-tree.adoc[236. Lowest Common Ancestor of a Binary Tree]。
6783

logbook/202406.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,10 @@
697697
|{doc_base_url}/0724-find-pivot-index.adoc[题解]
698698
|✅ 前缀和
699699

700+
|{counter:codes}
701+
|{leetcode_base_url}/lowest-common-ancestor-of-a-binary-search-tree/[235. Lowest Common Ancestor of a Binary Search Tree^]
702+
|{doc_base_url}/0235-lowest-common-ancestor-of-a-binary-search-tree.adoc[题解]
703+
|✅ 二叉搜索树左大右小,祖先在中间的特性,可以快速去除不必要的判断。
700704

701705
|===
702706

src/main/java/com/diguage/algo/leetcode/_0235_LowestCommonAncestorOfABinarySearchTree.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ public class _0235_LowestCommonAncestorOfABinarySearchTree {
1010
// tag::answer[]
1111
/**
1212
* 要充分利用二叉搜索树的特性
13+
*
14+
* @author D瓜哥 · https://www.diguage.com
15+
* @since 2024-06-24 18:21:20
1316
*/
1417
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
1518
TreeNode result = root;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.TreeNode;
4+
5+
public class _0235_LowestCommonAncestorOfABinarySearchTree_2 {
6+
// tag::answer[]
7+
/**
8+
* @author D瓜哥 · https://www.diguage.com
9+
* @since 2024-09-18 22:02:24
10+
*/
11+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
12+
if ((p.val <= root.val && root.val <= q.val)
13+
|| (q.val <= root.val && root.val <= p.val)) {
14+
return root;
15+
} else if (p.val <= root.val && q.val <= root.val) {
16+
return lowestCommonAncestor(root.left, p, q);
17+
} else {
18+
return lowestCommonAncestor(root.right, p, q);
19+
}
20+
}
21+
// end::answer[]
22+
}

0 commit comments

Comments
 (0)