-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLowestCommonAncestorOfABinaryTree.java
More file actions
45 lines (40 loc) · 1.51 KB
/
LowestCommonAncestorOfABinaryTree.java
File metadata and controls
45 lines (40 loc) · 1.51 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
package binary_tree;
/**
* @Author: Wenhang Chen
* @Description:给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
* <p>
* 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
* <p>
* 输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
* 输出: 3
* 解释: 节点 5 和节点 1 的最近公共祖先是节点 3。
* @Date: Created in 20:08 12/12/2019
* @Modified by:
*/
public class LowestCommonAncestorOfABinaryTree {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
// 存储LCA
private TreeNode ans;
private boolean recurseTree(TreeNode currentNode, TreeNode p, TreeNode q) {
if (currentNode == null) return false;
int left = this.recurseTree(currentNode.left, p, q) ? 1 : 0;
int right = this.recurseTree(currentNode.right, p, q) ? 1 : 0;
int mid = (currentNode == p || currentNode == q) ? 1 : 0;
// 如果左右子树或当前节点中有两个变为true
if (mid + left + right >= 2) {
this.ans = currentNode;
}
return (mid + left + right > 0);
}
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
this.recurseTree(root, p, q);
return this.ans;
}
}