-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
docs/1650-lowest-common-ancestor-of-a-binary-tree-iii.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
---- | ||
-- | ||
==== | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/com/diguage/algo/leetcode/_1650_LowestCommonAncestorOfABinaryTreeIII.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |