diff --git a/README.adoc b/README.adoc index 5e8134bb69..3795d3d473 100644 --- a/README.adoc +++ b/README.adoc @@ -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] diff --git a/docs/1650-lowest-common-ancestor-of-a-binary-tree-iii.adoc b/docs/1650-lowest-common-ancestor-of-a-binary-tree-iii.adoc new file mode 100644 index 0000000000..24cab22025 --- /dev/null +++ b/docs/1650-lowest-common-ancestor-of-a-binary-tree-iii.adoc @@ -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] +---- +-- +==== + + + diff --git a/docs/index.adoc b/docs/index.adoc index 91b6668697..6b51c685fb 100644 --- a/docs/index.adoc +++ b/docs/index.adoc @@ -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] diff --git a/logbook/202406.adoc b/logbook/202406.adoc index 4c079d39ee..816b7210ba 100644 --- a/logbook/202406.adoc +++ b/logbook/202406.adoc @@ -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} 道题。 diff --git a/src/main/java/com/diguage/algo/leetcode/_1650_LowestCommonAncestorOfABinaryTreeIII.java b/src/main/java/com/diguage/algo/leetcode/_1650_LowestCommonAncestorOfABinaryTreeIII.java new file mode 100644 index 0000000000..b18e2bd3a8 --- /dev/null +++ b/src/main/java/com/diguage/algo/leetcode/_1650_LowestCommonAncestorOfABinaryTreeIII.java @@ -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); + } + } +}