给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,null,3,5]
6
/ \
2 8
/ \ / \
0 4 7 9
/ \
3 5
难度:简单
示例 1:
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
输出: 6
解释: 节点 2 和节点 8 的最近公共祖先是 6。
示例 2:
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
输出: 2
解释: 节点 2 和节点 4 的最近公共祖先是 2, 因为根据定义最近公共祖先节点可以为节点本身。
这道题的关键在于利用二叉搜索树的特性:对于任意节点,其左子树上所有节点的值都小于该节点的值,其右子树上所有节点的值都大于该节点的值。
根据这一特性,我们可以设计以下算法:
这种方法的时间复杂度为 O(h),其中 h 是树的高度。对于平衡的二叉搜索树,时间复杂度为 O(log n);空间复杂度为 O(1)。
递归方法的思路类似:
这种方法的时间复杂度也为 O(h);空间复杂度为 O(h),主要是由于递归调用栈的深度。
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
TreeNode current = root;
while (current != null) {
// 如果p和q都在current的右子树中
if (p.val > current.val && q.val > current.val) {
current = current.right;
}
// 如果p和q都在current的左子树中
else if (p.val < current.val && q.val < current.val) {
current = current.left;
}
// 找到了最近公共祖先
else {
return current;
}
}
return null;
}
}
# 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':
current = root
while current:
# 如果p和q都在current的右子树中
if p.val > current.val and q.val > current.val:
current = current.right
# 如果p和q都在current的左子树中
elif p.val < current.val and q.val < current.val:
current = current.left
# 找到了最近公共祖先
else:
return current
return None
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
TreeNode* current = root;
while (current != nullptr) {
// 如果p和q都在current的右子树中
if (p->val > current->val && q->val > current->val) {
current = current->right;
}
// 如果p和q都在current的左子树中
else if (p->val < current->val && q->val < current->val) {
current = current->left;
}
// 找到了最近公共祖先
else {
return current;
}
}
return nullptr;
}
};
public class Solution {
public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
// 如果p和q都在root的右子树中
if (p.val > root.val && q.val > root.val) {
return LowestCommonAncestor(root.right, p, q);
}
// 如果p和q都在root的左子树中
else if (p.val < root.val && q.val < root.val) {
return LowestCommonAncestor(root.left, p, q);
}
// 找到了最近公共祖先
else {
return root;
}
}
}
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
# 如果p和q都在root的右子树中
if p.val > root.val and q.val > root.val:
return self.lowestCommonAncestor(root.right, p, q)
# 如果p和q都在root的左子树中
elif p.val < root.val and q.val < root.val:
return self.lowestCommonAncestor(root.left, p, q)
# 找到了最近公共祖先
else:
return root
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
// 如果p和q都在root的右子树中
if (p->val > root->val && q->val > root->val) {
return lowestCommonAncestor(root->right, p, q);
}
// 如果p和q都在root的左子树中
else if (p->val < root->val && q->val < root->val) {
return lowestCommonAncestor(root->left, p, q);
}
// 找到了最近公共祖先
else {
return root;
}
}
};
方法 | 时间复杂度 | 空间复杂度 | 优势 | 劣势 |
---|---|---|---|---|
迭代 | O(h) | O(1) | 空间复杂度低,不受树高影响 | 代码略微复杂 |
递归 | O(h) | O(h) | 代码清晰简洁 | 对于深层树,可能导致栈溢出 |
以下数据基于LeetCode标准测试环境(实际值可能有所不同):
语言 | 迭代法耗时 | 递归法耗时 |
---|---|---|
C++ | ~16ms | ~20ms |
C# | ~100ms | ~108ms |
Python | ~76ms | ~80ms |
C++实现由于其低级别特性和直接内存管理,通常表现出最佳性能。Python和C#由于其高级语言特性和垃圾回收机制,执行较慢,但提供了更好的开发体验。
此题的算法已经相当优化,由于充分利用了二叉搜索树的特性,时间复杂度已达最优O(h)。不过,可以考虑以下细微优化: