Skip to content

Commit 912f246

Browse files
committed
一刷1650
1 parent 800a9a8 commit 912f246

File tree

5 files changed

+151
-0
lines changed

5 files changed

+151
-0
lines changed

README.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9420,6 +9420,13 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
94209420
|Medium
94219421
|树形DP套路,没有验证答案!
94229422

9423+
|{counter:codes}
9424+
|{leetcode_base_url}/lowest-common-ancestor-of-a-binary-tree-iii/[1650. Lowest Common Ancestor of a Binary Tree III^]
9425+
|{source_base_url}/_1650_LowestCommonAncestorOfABinaryTreeIII.java[Java]
9426+
|{doc_base_url}/1650-lowest-common-ancestor-of-a-binary-tree-iii.adoc[题解]
9427+
|Medium
9428+
|链表相交
9429+
94239430
|{counter:codes}
94249431
|{leetcode_base_url}/count-sub-islands/[1905. Count Sub Islands]
94259432
|{source_base_url}/_1905_CountSubIslands.java[Java]
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
[#1650-lowest-common-ancestor-of-a-binary-tree-iii]
2+
= 1650. Lowest Common Ancestor of a Binary Tree III
3+
4+
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-iii/[LeetCode - 1650. Lowest Common Ancestor of a Binary Tree III ^]
5+
6+
Given two nodes of a binary tree `p` and `q`, return **their lowest common ancestor (LCA)**.
7+
8+
Each node will have a reference to its parent node. The definition for `Node` is below:
9+
10+
[{java_src_attr}]
11+
----
12+
class Node {
13+
public int val;
14+
public Node left;
15+
public Node right;
16+
public Node parent;
17+
}
18+
----
19+
20+
According to the https://en.wikipedia.org/wiki/Lowest_common_ancestor[definition of LCA on Wikipedia^]: The lowest common ancestor of two nodes p and q in a tree T is the lowest node that has both p and q as descendants (where we allow a node to be a descendant of itself).
21+
22+
23+
**Example 1:**
24+
25+
image::https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1600-1699/1650.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree%20III/images/binarytree.png[]
26+
27+
----
28+
**Input:** root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
29+
**Output:** 3
30+
**Explanation:** The LCA of nodes 5 and 1 is 3.
31+
----
32+
33+
**Example 2:**
34+
35+
image::https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1600-1699/1650.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree%20III/images/binarytree.png[]
36+
37+
----
38+
**Input:** root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
39+
**Output:** 5
40+
**Explanation:** The LCA of nodes 5 and 4 is 5 since a node can be a descendant of itself according to the LCA definition.
41+
----
42+
43+
**Example 3:**
44+
45+
----
46+
**Input:** root = [1,2], p = 1, q = 2
47+
**Output:** 1
48+
----
49+
50+
**Constraints:**
51+
52+
* The number of nodes in the tree is in the range `[2, 10^5^]`.
53+
* `-10^9^ <= Node.val <= 10^9^`
54+
* All `Node.val` are **unique**.
55+
* `p != q`
56+
* `p` and `q` exist in the tree.
57+
58+
59+
== 解题分析
60+
61+
这道题披着树的外衣,里面确实一个链接相交的芯。
62+
63+
[[src-1650]]
64+
[tabs]
65+
====
66+
一刷::
67+
+
68+
--
69+
[{java_src_attr}]
70+
----
71+
include::{sourcedir}/_1650_LowestCommonAncestorOfABinaryTreeIII.java[tag=answer]
72+
----
73+
--
74+
====
75+
76+
77+

docs/index.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2744,6 +2744,8 @@ include::1340-jump-game-v.adoc[leveloffset=+1]
27442744

27452745
include::1644-lowest-common-ancestor-of-a-binary-tree-ii.adoc[leveloffset=+1]
27462746

2747+
include::1650-lowest-common-ancestor-of-a-binary-tree-iii.adoc[leveloffset=+1]
2748+
27472749
include::1905-count-sub-islands.adoc[leveloffset=+1]
27482750

27492751
include::0000-00-note.adoc[leveloffset=+1]

logbook/202406.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,11 @@
347347
|{doc_base_url}/0042-trapping-rain-water.adoc[题解]
348348
|巧用单调栈
349349

350+
|{counter:codes}
351+
|{leetcode_base_url}/lowest-common-ancestor-of-a-binary-tree-iii/[1650. Lowest Common Ancestor of a Binary Tree III^]
352+
|{doc_base_url}/1650-lowest-common-ancestor-of-a-binary-tree-iii.adoc[题解]
353+
|链表相交
354+
350355
|===
351356

352357
截止目前,本轮练习一共完成 {codes} 道题。
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.Objects;
4+
5+
public class _1650_LowestCommonAncestorOfABinaryTreeIII {
6+
// tag::answer[]
7+
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2024-07-26 20:00:58
11+
*/
12+
public TreeNode lowestCommonAncestor(TreeNode p, TreeNode q) {
13+
if (Objects.isNull(p) || Objects.isNull(q)) {
14+
return null;
15+
}
16+
TreeNode a = p;
17+
TreeNode b = q;
18+
while (a != b) {
19+
if (a == null) {
20+
a = q;
21+
} else {
22+
a = a.parent;
23+
}
24+
if (b == null) {
25+
b = p;
26+
} else {
27+
b = b.parent;
28+
}
29+
}
30+
return a;
31+
}
32+
// end::answer[]
33+
34+
public class TreeNode {
35+
public int val;
36+
public TreeNode left;
37+
public TreeNode right;
38+
public TreeNode parent;
39+
40+
public TreeNode() {
41+
}
42+
43+
public TreeNode(int x) {
44+
val = x;
45+
}
46+
47+
@Override
48+
public boolean equals(Object o) {
49+
if (this == o) return true;
50+
if (o == null || getClass() != o.getClass()) return false;
51+
TreeNode treeNode = (TreeNode) o;
52+
return val == treeNode.val;
53+
}
54+
55+
@Override
56+
public int hashCode() {
57+
return Objects.hash(val);
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)