Skip to content

Commit

Permalink
一刷1650
Browse files Browse the repository at this point in the history
  • Loading branch information
diguage committed Aug 14, 2024
1 parent 800a9a8 commit 912f246
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9420,6 +9420,13 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
|Medium
|树形DP套路,没有验证答案!

|{counter:codes}
|{leetcode_base_url}/lowest-common-ancestor-of-a-binary-tree-iii/[1650. Lowest Common Ancestor of a Binary Tree III^]
|{source_base_url}/_1650_LowestCommonAncestorOfABinaryTreeIII.java[Java]
|{doc_base_url}/1650-lowest-common-ancestor-of-a-binary-tree-iii.adoc[题解]
|Medium
|链表相交

|{counter:codes}
|{leetcode_base_url}/count-sub-islands/[1905. Count Sub Islands]
|{source_base_url}/_1905_CountSubIslands.java[Java]
Expand Down
77 changes: 77 additions & 0 deletions docs/1650-lowest-common-ancestor-of-a-binary-tree-iii.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
[#1650-lowest-common-ancestor-of-a-binary-tree-iii]
= 1650. Lowest Common Ancestor of a Binary Tree III

https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-iii/[LeetCode - 1650. Lowest Common Ancestor of a Binary Tree III ^]

Given two nodes of a binary tree `p` and `q`, return **their lowest common ancestor (LCA)**.

Each node will have a reference to its parent node. The definition for `Node` is below:

[{java_src_attr}]
----
class Node {
public int val;
public Node left;
public Node right;
public Node parent;
}
----

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).


**Example 1:**

image::https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1600-1699/1650.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree%20III/images/binarytree.png[]

----
**Input:** root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
**Output:** 3
**Explanation:** The LCA of nodes 5 and 1 is 3.
----

**Example 2:**

image::https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1600-1699/1650.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree%20III/images/binarytree.png[]

----
**Input:** root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
**Output:** 5
**Explanation:** The LCA of nodes 5 and 4 is 5 since a node can be a descendant of itself according to the LCA definition.
----

**Example 3:**

----
**Input:** root = [1,2], p = 1, q = 2
**Output:** 1
----

**Constraints:**

* The number of nodes in the tree is in the range `[2, 10^5^]`.
* `-10^9^ <= Node.val <= 10^9^`
* All `Node.val` are **unique**.
* `p != q`
* `p` and `q` exist in the tree.


== 解题分析

这道题披着树的外衣,里面确实一个链接相交的芯。

[[src-1650]]
[tabs]
====
一刷::
+
--
[{java_src_attr}]
----
include::{sourcedir}/_1650_LowestCommonAncestorOfABinaryTreeIII.java[tag=answer]
----
--
====



2 changes: 2 additions & 0 deletions docs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2744,6 +2744,8 @@ include::1340-jump-game-v.adoc[leveloffset=+1]

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

include::1650-lowest-common-ancestor-of-a-binary-tree-iii.adoc[leveloffset=+1]

include::1905-count-sub-islands.adoc[leveloffset=+1]

include::0000-00-note.adoc[leveloffset=+1]
5 changes: 5 additions & 0 deletions logbook/202406.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@
|{doc_base_url}/0042-trapping-rain-water.adoc[题解]
|巧用单调栈

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

|===

截止目前,本轮练习一共完成 {codes} 道题。
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.diguage.algo.leetcode;

import java.util.Objects;

public class _1650_LowestCommonAncestorOfABinaryTreeIII {
// tag::answer[]

/**
* @author D瓜哥 · https://www.diguage.com
* @since 2024-07-26 20:00:58
*/
public TreeNode lowestCommonAncestor(TreeNode p, TreeNode q) {
if (Objects.isNull(p) || Objects.isNull(q)) {
return null;
}
TreeNode a = p;
TreeNode b = q;
while (a != b) {
if (a == null) {
a = q;
} else {
a = a.parent;
}
if (b == null) {
b = p;
} else {
b = b.parent;
}
}
return a;
}
// end::answer[]

public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode parent;

public TreeNode() {
}

public TreeNode(int x) {
val = x;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TreeNode treeNode = (TreeNode) o;
return val == treeNode.val;
}

@Override
public int hashCode() {
return Objects.hash(val);
}
}
}

0 comments on commit 912f246

Please sign in to comment.