Skip to content

Commit 9ca9af7

Browse files
committed
三刷236
1 parent e9671ba commit 9ca9af7

5 files changed

+56
-10
lines changed

Diff for: docs/0000-00-note.adoc

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ image::images/quick-sort-01.gif[{image_attr}]
2222
== TODO
2323

2424
* [ ] https://leetcode.cn/problems/number-of-islands/solutions/211211/dao-yu-lei-wen-ti-de-tong-yong-jie-fa-dfs-bian-li-/[200. 岛屿数量 - 岛屿类问题的通用解法、DFS 遍历框架^]
25+
* [ ] 魔改的二分查找
26+
* [ ] 买卖股票
2527

2628
== 解题范式
2729

Diff for: docs/0236-lowest-common-ancestor-of-a-binary-tree.adoc

+26-10
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,14 @@
44

55
{leetcode}/problems/lowest-common-ancestor-of-a-binary-tree/[LeetCode - Lowest Common Ancestor of a Binary Tree^]
66

7-
image::images/0236-1.png[{image_attr}]
8-
9-
D瓜哥的思路:先找出一条从根节点到某个节点的路径;然后从这条路径上以此去寻找另外一个节点。找到这返回此节点。
10-
11-
思考题:如何按照"路径"的思路实现一遍?
12-
13-
== 解题分析
14-
157
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
168

179
According to the https://en.wikipedia.org/wiki/Lowest_common_ancestor[definition of LCA on Wikipedia^]: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow *a node to be a descendant of itself*).”
1810

1911
Given the following binary tree: root = [3,5,1,6,2,0,8,null,null,7,4]
2012

21-
image::https://assets.leetcode.com/uploads/2018/12/14/binarytree.png[{image_attr}]
13+
image::images/0236-1.png[{image_attr}]
2214

23-
2415
*Example 1:*
2516

2617
[subs="verbatim,quotes,macros"]
@@ -47,8 +38,13 @@ image::https://assets.leetcode.com/uploads/2018/12/14/binarytree.png[{image_attr
4738
* All of the nodes' values will be unique.
4839
* p and q are different and both values will exist in the binary tree.
4940

41+
5042
== 思路分析
5143

44+
D瓜哥的思路:先找出一条从根节点到某个节点的路径;然后从这条路径上以此去寻找另外一个节点。找到这返回此节点。
45+
46+
思考题:如何按照"路径"的思路实现一遍?
47+
5248
这道题是 xref:0235-lowest-common-ancestor-of-a-binary-search-tree.adoc[235. Lowest Common Ancestor of a Binary Search Tree] 的延伸。但是,解题思路略有不同,本体的解题思路也可用于前者。
5349

5450
有两种情况:
@@ -104,15 +100,35 @@ image::images/0236-18.png[{image_attr}]
104100

105101

106102
[[src-0236]]
103+
[tabs]
104+
====
105+
一刷::
106+
+
107+
--
107108
[{java_src_attr}]
108109
----
109110
include::{sourcedir}/_0236_LowestCommonAncestorOfABinaryTree.java[tag=answer]
110111
----
112+
--
111113
114+
二刷::
115+
+
116+
--
112117
[{java_src_attr}]
113118
----
114119
include::{sourcedir}/_0236_LowestCommonAncestorOfABinaryTree_2.java[tag=answer]
115120
----
121+
--
122+
123+
三刷::
124+
+
125+
--
126+
[{java_src_attr}]
127+
----
128+
include::{sourcedir}/_0236_LowestCommonAncestorOfABinaryTree_3.java[tag=answer]
129+
----
130+
--
131+
====
116132

117133
== 参考资料
118134

Diff for: logbook/202406.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,11 @@
565565
|{doc_base_url}/0704-binary-search.adoc[题解]
566566
|二分查找
567567

568+
|{counter:codes}
569+
|{leetcode_base_url}/lowest-common-ancestor-of-a-binary-tree/[236. Lowest Common Ancestor of a Binary Tree^]
570+
|{doc_base_url}/0236-lowest-common-ancestor-of-a-binary-tree.adoc[题解]
571+
|想清楚怎么判断是否为公共祖先。
572+
568573
|===
569574

570575
截止目前,本轮练习一共完成 {codes} 道题。

Diff for: src/main/java/com/diguage/algo/leetcode/_0236_LowestCommonAncestorOfABinaryTree_2.java

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public class _0236_LowestCommonAncestorOfABinaryTree_2 {
4242
// tag::answer[]
4343
/**
4444
* 参考 左程云《程序员代码面试指南》的解法
45+
*
46+
* @author D瓜哥 · https://www.diguage.com
47+
* @since 2024-06-24 21:08:59
4548
*/
4649
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
4750
if (root == null || root == p || root == q) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.TreeNode;
4+
5+
public class _0236_LowestCommonAncestorOfABinaryTree_3 {
6+
// tag::answer[]
7+
/**
8+
* @author D瓜哥 · https://www.diguage.com
9+
* @since 2024-09-14 21:42:37
10+
*/
11+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
12+
if (root == null || root.val == p.val || root.val == q.val) {
13+
return root;
14+
}
15+
TreeNode left = lowestCommonAncestor(root.left, p, q);
16+
TreeNode right = lowestCommonAncestor(root.right, p, q);
17+
return left == null ? (right == null ? null : right) : left;
18+
}
19+
// end::answer[]
20+
}

0 commit comments

Comments
 (0)