题目描述:
给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。最近公共祖先的定义为:“对于有根树 T 的两个节点 p、q,最近公共祖先表示为一个节点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
示例 :
输入: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.
way:2个节点a和b的最近公共祖先说的是,a和b都往上跑,最低都会遇到的某个节点,a和b节点其中一个可能是祖先。那么我们可以先序遍历这棵树,将所有节点的父亲节点都存储到map中,那么每个节点都可以顺着自己往上找自己的父亲节点,然后假设我们求p和q的最近公共祖先,就可以先从p出发,往上走直到根节点并把访问过的节点记录到map中,然后再从q出发往上跑,如果访问到之前p已经访问过的节点,那么这个节点就是p和q的最低公共祖先。
/**
* 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:
unordered_map<TreeNode *, TreeNode*>fa;
unordered_map<TreeNode *, bool>vis;
void findFather(TreeNode * root)
{
if(root == nullptr) return;
if(root->left)
{
fa[root->left] = root;
findFather(root->left);
}
if(root->right)
{
fa[root->right] = root;
findFather(root->right);
}
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
findFather(root);
fa[root] = nullptr;
while(p!=nullptr)
{
vis[p] = true;
p = fa[p];
}
while(q!=nullptr)
{
if(vis[q]) return q;
q = fa[q];
}
return nullptr;
}
};
way:如果向左子树和右子树分别要信息得到自身信息返回的话,如果以x为根节点的树,假设找节点a和b的最近公共祖先,如果祖先经过x,那么可能是x的左子树有a,右子树有b(1),x是a,左子树或右子树有b(2),x是b,左子树或右子树有a(3),如果祖先不经过x,那么可能在左子树上找到了a和b的最近公共祖先,可能在右子树上找到了a和b的最近公共祖先,所以我们向左右子树要的信息有,是否有a,是否有b,是否在树上已经找到了a和b的最近公共祖先(如果找到了就存起这个答案)。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
struct Info{
bool findA;
bool findB;
TreeNode *ancestor;
Info(bool findA, bool findB, TreeNode* ancestor)
{
this->findA = findA;
this->findB = findB;
this->ancestor = ancestor;
}
Info():findA(false),findB(false),ancestor(nullptr)
{
}
};
Info getInfo(TreeNode *root, TreeNode *p, TreeNode *q)
{
if(root == nullptr) return Info();
Info left = getInfo(root->left, p, q);
Info right = getInfo(root->right, p, q);
bool findA=false,findB=false;
TreeNode * ancestor = nullptr;
if(left.findA || right.findA || root->val == p->val)
{
findA = true;
}
if(left.findB || right.findB || root->val == q->val)
{
findB = true;
}
if(left.ancestor)
{
ancestor = left.ancestor;
}else if(right.ancestor)
{
ancestor = right.ancestor;
}else
{
if(findA && findB)
{
ancestor = root;
}
}
return Info(findA, findB, ancestor);
}
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
return getInfo(root,p,q).ancestor;
}
};
最后附上Leetcode上的这种写法吧,和上面我说的意思是一样的,这里它的dfs返回的是p或q节点有没有在当前树中出现过(没有区分到底是哪一个,因为它的ans也是只要2个都有就行,2个都有那就是一定有)。
class Solution {
public:
TreeNode* ans;
bool dfs(TreeNode* root, TreeNode* p, TreeNode* q) {
if (root == nullptr) return false;
bool lson = dfs(root->left, p, q);
bool rson = dfs(root->right, p, q);
if ((lson && rson) || ((root->val == p->val || root->val == q->val) && (lson || rson))) {
ans = root;
}
return lson || rson || (root->val == p->val || root->val == q->val);
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
dfs(root, p, q);
return ans;
}
};