题意描述:
给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
例如,给定如下二叉树: root = [3,5,1,6,2,0,8,null,null,7,4]
示例:
示例一:
输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
输出: 3
解释: 节点 5 和节点 1 的最近公共祖先是节点 3。
示例二:
输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
输出: 5
解释: 节点 5 和节点 4 的最近公共祖先是节点 5。因为根据定义最近公共祖先节点可以为节点本身。
解题思路:
Alice: 如何 ?
Bob: 这种题目应该会很常见的,求一个二叉树中两个节点的最近公共祖先,题目又简洁又好懂,而且不好做。
Alice: 那你觉得应该怎么做 ?
Bob:求公共祖先好说,求最近的公共祖先就不好说了,二叉树只有从父节点指向两个子节点的关系而没有反向的关系。
Alice:是啊,关键是如何判断一个节点是不是 节点 p 和 节点 q 的最近公共祖先
。
Bob: 从 p 和 q 的祖先节点出发一定能访问到 p 和 q 这两个节点的,如果不是就不行。
Alice: hhh,我知道了,p 和 q 的最近公共祖先满足这个条件: 从这个节点出发能够访问到 p 和 q,但是从该节点的两个子节点出发不能访问到两个节点。
Bob: 拿示例 2 来举例子就是说 从 5 节点除法能够访问到 p 和 q 而 5 的从5的两个子节点出发都不能同时访问到 p 和 q。
Alice:还有一个问题,如何判断 从一个节点出发能不能访问到 p 和 q 呢
?
Bob: 用两个全局变量打标记
呀,这样还能做点优化,访问到 两个节点之后提前停止对二叉树中其余节点的访问。
Alice: emm, 理论上来说是行的,就怕会超时啊,我们这样子应该是近似O(n^2) 的算法了。要多次访问二叉树里面的同一个节点。
Bob: 试试呗,说不定就过了呢。
Alice: 🐶🐶
代码:
Python 方法一: 暴力求解 + 超时测试点优化。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if self.isAncestor(p, p, q):
return p
if self.isAncestor(q, p, q):
return q
return self.lowestCommonAncestorHelper(root, p, q)
def lowestCommonAncestorHelper(self, root, p, q):
rootValue = self.isAncestor(root, p, q)
leftValue = self.isAncestor(root.left, p, q)
rightValue = self.isAncestor(root.right, p, q)
if rootValue and not leftValue and not rightValue:
return root
elif rootValue and leftValue and not rightValue:
return self.lowestCommonAncestorHelper(root.left, p, q)
elif rootValue and not leftValue and rightValue:
return self.lowestCommonAncestorHelper(root.right, p, q)
else:
print("something worng")
def isAncestor(self, root, p, q):
self.left = False
self.right = False
self.isAncestorHelper(root, p, q)
if self.left and self.right:
return True
else:
return False
def isAncestorHelper(self, root, p, q):
if self.left == True and self.right == True:
return
if root == None:
return
else:
if root.val == p.val:
self.left = True
elif root.val == q.val:
self.right = True
self.isAncestorHelper(root.left, p, q)
self.isAncestorHelper(root.right, p, q)
Java 方法一: 暴力 + 针对特定测试用例优化。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
boolean left = false;
boolean right = false;
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(this.isAncestor(p, p, q)){
return p;
}else if(this.isAncestor(q, p, q)){
return q;
}else{
return this.lowestCommonAncestorHelper(root, p, q);
}
}
public TreeNode lowestCommonAncestorHelper(TreeNode root, TreeNode p, TreeNode q){
// rootValue always true, deleted
boolean leftValue = this.isAncestor(root.left, p, q);
boolean rightValue = this.isAncestor(root.right, p, q);
if(leftValue == false && rightValue == false){
return root;
}else if(leftValue == true && rightValue == false){
return this.lowestCommonAncestorHelper(root.left, p, q);
}else if(leftValue == false && rightValue == true){
return this.lowestCommonAncestorHelper(root.right, p, q);
}else{
// nothing, should be never exec
}
return null;
//should be never exec
}
public boolean isAncestor(TreeNode root, TreeNode p, TreeNode q){
this.left = false;
this.right = false;
this.isAncestorHelper(root, p, q);
if(this.left && this.right){
return true;
}else{
return false;
}
}
public void isAncestorHelper(TreeNode root, TreeNode p, TreeNode q){
// modify this.left and this.right value
if(this.left == true && this.right == true){
return ;
}
if(root == null){
return ;
}else{
if(root.val == p.val){
this.left = true;
}
if(root.val == q.val){
this.right = true;
}
this.isAncestorHelper(root.left, p, q);
this.isAncestorHelper(root.right, p, q);
}
}
}
易错点:
- 易超时。
总结:
- LeetCode 的测试样例可能真的有些弱。