二叉树的最近公共祖先 中等
课后作业
问题描述
原文链接:236. 二叉树的最近公共祖先
给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个节点 p、q,最近公共祖先表示为一个节点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
示例 1:

输入:root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
输出:3
解释:节点 5 和节点 1 的最近公共祖先是节点 3 。
示例 2:

输入:root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
输出:5
解释:节点 5 和节点 4 的最近公共祖先是节点 5 。因为根据定义最近公共祖先节点可以为节点本身。
示例 3:
输入:root = [1,2], p = 1, q = 2
输出:1
提示:
- 树中节点数目在范围
[2, 105]内。 -109 <= Node.val <= 109- 所有
Node.val互不相同。 p != qp和q均存在于给定的二叉树中。
代码实现
Java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null){
return null;
}
//情况1:如果 p 或者 q 和根重合,说明此时 根为最近祖先
if(root.val == p.val || root.val == q.val){
return root;
}
// 目的:缩小范围
// p,q是否都在root的左子树
if(find(root.left, q) && find(root.left, p)){
return lowestCommonAncestor(root.left, p, q);
}
// p,q是否都在root的右子树
if(find(root.right, q) && find(root.right, p)){
return lowestCommonAncestor(root.right, p, q);
}
return root;
}
// 判断 t 是否是 root 的子树
boolean find(TreeNode root, TreeNode t){
if(root == null){
return false;
}
if(root.val == t.val){
return true;
}
return find(root.left, t) || find(root.right, t);
}
}
Python
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def lowestCommonAncestor(self, root, p, q):
"""
:type root: TreeNode
:type p: TreeNode
:type q: TreeNode
:rtype: TreeNode
"""
if not root:
return None
# 情况1: 如果 p 或者 q 和根重合,说明此时 根为最近祖先
if root.val == p.val or root.val == q.val:
return root
# 目的:缩小范围
# p, q 是否都在 root 的左子树
if self.find(root.left, q) and self.find(root.left, p):
return self.lowestCommonAncestor(root.left, p, q)
# p, q 是否都在 root 的右子树
if self.find(root.right, q) and self.find(root.right, p):
return self.lowestCommonAncestor(root.right, p, q)
return root
# 判断 t 是否是 root 的子树
def find(self, root, t):
if not root:
return False
if root.val == t.val:
return True
return self.find(root.left, t) or self.find(root.right, t)
C++
/**
* 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) {
if (!root) {
return nullptr;
}
// 情况1: 如果 p 或者 q 和根重合,说明此时 根为最近祖先
if (root->val == p->val || root->val == q->val) {
return root;
}
// 目的:缩小范围
// p, q 是否都在 root 的左子树
if (find(root->left, q) && find(root->left, p)) {
return lowestCommonAncestor(root->left, p, q);
}
// p, q 是否都在 root 的右子树
if (find(root->right, q) && find(root->right, p)) {
return lowestCommonAncestor(root->right, p, q);
}
return root;
}
// 判断 t 是否是 root 的子树
bool find(TreeNode* root, TreeNode* t) {
if (!root) {
return false;
}
if (root->val == t->val) {
return true;
}
return find(root->left, t) || find(root->right, t);
}
};
Go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
if root == nil {
return nil
}
// 情况1: 如果 p 或者 q 和根重合,说明此时 根为最近祖先
if root.Val == p.Val || root.Val == q.Val {
return root
}
// 目的:缩小范围
// p, q 是否都在 root 的左子树
if find(root.Left, q) && find(root.Left, p) {
return lowestCommonAncestor(root.Left, p, q)
}
// p, q 是否都在 root 的右子树
if find(root.Right, q) && find(root.Right, p) {
return lowestCommonAncestor(root.Right, p, q)
}
return root
}
// 判断 t 是否是 root 的子树
func find(root, t *TreeNode) bool {
if root == nil {
return false
}
if root.Val == t.Val {
return true
}
return find(root.Left, t) || find(root.Right, t)
}