@@ -21,50 +21,54 @@ tags:
21
21
22
22
<p >给定一棵二叉树中的两个节点 <code >p</code > 和 <code >q</code >,返回它们的最近公共祖先节点(LCA)。</p >
23
23
24
- <p >每个节点都包含其父节点的引用(指针)。<code >Node</code > 的定义如下:</p >
24
+ <p >每个节点都包含其父节点的引用(指针)。<code >Node</code >& nbsp ; 的定义如下:</p >
25
25
26
- <pre >class Node {
26
+ <pre >
27
+ class Node {
27
28
public int val;
28
29
public Node left;
29
30
public Node right;
30
31
public Node parent;
31
32
}
32
33
</pre >
33
34
34
- <p >根据<a href =" https://en.wikipedia.org/wiki/Lowest_common_ancestor " >维基百科中对最近公共祖先节点的定义</a >:“两个节点 p 和 q 在二叉树 T 中的最近公共祖先节点是后代节点中既包括 p 又包括 q 的最深节点(我们允许<strong >一个节点为自身的一个后代节点</strong >)”。一个节点 x 的后代节点是节点 x 到某一叶节点间的路径中的节点 y。</p >
35
+ <p >根据<a href =" https://en.wikipedia.org/wiki/Lowest_common_ancestor " >维基百科中对最近公共祖先节点的定义</a >:“两个节点 p 和 q 在二叉树 T 中的最近公共祖先节点是后代节点中既包括 p& nbsp ; 又包括& nbsp ; q & nbsp ; 的最深节点(我们允许<strong >一个节点为自身的一个后代节点</strong >)”。一个节点 x& nbsp ; 的后代节点是节点& nbsp ; x 到某一叶节点间的路径中的节点 y。</p >
35
36
36
- <p > </p >
37
+ <p >& nbsp ; </p >
37
38
38
- <p ><strong >示例 1:</strong ></p >
39
- <img alt =" " src =" https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1600-1699/1650.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree%20III/images/binarytree.png " style =" width : 200px ; height : 190px ;" >
40
- <pre ><strong >输入:</strong > root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
39
+ <p ><strong class =" example " >示例 1:</strong ></p >
40
+ <img alt =" " src =" https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1600-1699/1650.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree%20III/images/binarytree.png " style =" width : 200px ; height : 190px ;" />
41
+ <pre >
42
+ <strong >输入:</strong > root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
41
43
<strong >输出:</strong > 3
42
44
<strong >解释:</strong > 节点 5 和 1 的最近公共祖先是 3。
43
45
</pre >
44
46
45
- <p ><strong >示例 2:</strong ></p >
46
- <img alt =" " src =" https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1600-1699/1650.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree%20III/images/binarytree.png " style =" width : 200px ; height : 190px ;" >
47
- <pre ><strong >输入:</strong > root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
47
+ <p ><strong class =" example " >示例 2:</strong ></p >
48
+ <img alt =" " src =" https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1600-1699/1650.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree%20III/images/binarytree.png " style =" width : 200px ; height : 190px ;" />
49
+ <pre >
50
+ <strong >输入:</strong > root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
48
51
<strong >输出:</strong > 5
49
52
<strong >解释:</strong > 节点 5 和 4 的最近公共祖先是 5,根据定义,一个节点可以是自身的最近公共祖先。
50
53
</pre >
51
54
52
- <p ><strong >示例 3:</strong ></p >
55
+ <p ><strong class = " example " >示例 3:</strong ></p >
53
56
54
- <pre ><strong >输入:</strong > root = [1,2], p = 1, q = 2
57
+ <pre >
58
+ <strong >输入:</strong > root = [1,2], p = 1, q = 2
55
59
<strong >输出:</strong > 1
56
60
</pre >
57
61
58
- <p > </p >
62
+ <p >& nbsp ; </p >
59
63
60
64
<p ><strong >提示:</strong ></p >
61
65
62
66
<ul >
63
- <li>树中节点个数的范围是 <code>[2, 10<sup>5</sup>]</code>。</li>
67
+ <li>树中节点个数的范围是 <code>[2, 10<sup>5</sup>]</code>。</li>
64
68
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
65
- <li>所有的 <code>Node.val</code> 都是<strong>互不相同</strong>的。</li>
69
+ <li>所有的 <code>Node.val</code> 都是<strong>互不相同</strong>的。</li>
66
70
<li><code>p != q</code></li>
67
- <li><code>p</code> 和 <code>q</code> 存在于树中。</li>
71
+ <li><code>p</code> 和 <code>q</code> 存在于树中。</li>
68
72
</ul >
69
73
70
74
<!-- description:end -->
0 commit comments