-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLowestCommonAncestorOfABinaryTree.java
More file actions
83 lines (77 loc) · 3.17 KB
/
Copy pathLowestCommonAncestorOfABinaryTree.java
File metadata and controls
83 lines (77 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* <p>给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。</p>
*
* <p><a href="https://baike.baidu.com/item/%E6%9C%80%E8%BF%91%E5%85%AC%E5%85%B1%E7%A5%96%E5%85%88/8918834?fr=aladdin" target="_blank">百度百科</a>中最近公共祖先的定义为:“对于有根树 T 的两个节点 p、q,最近公共祖先表示为一个节点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(<strong>一个节点也可以是它自己的祖先</strong>)。”</p>
*
* <p> </p>
*
* <p><strong>示例 1:</strong></p>
* <img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
* <pre>
* <strong>输入:</strong>root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
* <strong>输出:</strong>3
* <strong>解释:</strong>节点 <code>5 </code>和节点 <code>1 </code>的最近公共祖先是节点 <code>3 。</code>
* </pre>
*
* <p><strong>示例 2:</strong></p>
* <img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
* <pre>
* <strong>输入:</strong>root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
* <strong>输出:</strong>5
* <strong>解释:</strong>节点 <code>5 </code>和节点 <code>4 </code>的最近公共祖先是节点 <code>5 。</code>因为根据定义最近公共祖先节点可以为节点本身。
* </pre>
*
* <p><strong>示例 3:</strong></p>
*
* <pre>
* <strong>输入:</strong>root = [1,2], p = 1, q = 2
* <strong>输出:</strong>1
* </pre>
*
* <p> </p>
*
* <p><strong>提示:</strong></p>
*
* <ul>
* <li>树中节点数目在范围 <code>[2, 10<sup>5</sup>]</code> 内。</li>
* <li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
* <li>所有 <code>Node.val</code> <code>互不相同</code> 。</li>
* <li><code>p != q</code></li>
* <li><code>p</code> 和 <code>q</code> 均存在于给定的二叉树中。</li>
* </ul>
* <div><div>Related Topics</div><div><li>树</li><li>深度优先搜索</li><li>二叉树</li></div></div><br><div><li>👍 1398</li><li>👎 0</li></div>
*/
package leetcode3;
public class LowestCommonAncestorOfABinaryTree {
public static void main(String[] args) {
Solution solution = new LowestCommonAncestorOfABinaryTree().new Solution();
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
/**
* 可参考这篇题解:https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/solution/236-er-cha-shu-de-zui-jin-gong-gong-zu-xian-hou-xu/
* DFS
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null || root == p || root == q) {
return root;
}
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
if (left == null) {
return right;
}
if (right == null) {
return left;
}
return root;
}
}
}