题目
给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
例如,给定如下二叉树: root = [3,5,1,6,2,0,8,null,null,7,4]
说明:
所有节点的值都是唯一的。
p、q 为不同节点且均存在于给定的二叉树中。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解法1:
分别找到从根到俩个节点的路径,俩条路径中相等的节点叫公共祖先,离根最远的叫最近公共祖先。
/**
* 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:
void preOrder(TreeNode* root,TreeNode* searchNode,vector<TreeNode*> &path_tree,vector<TreeNode*> &result_path,int &finish)
{
if(root==nullptr||finish) return;
path_tree.push_back(root);
if(root==searchNode)
{
result_path=path_tree;
finish=1;
}
preOrder(root->left,searchNode,path_tree,result_path,finish);
preOrder(root->right,searchNode,path_tree,result_path,finish);
path_tree.pop_back();
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root==nullptr) return nullptr;
int finish=0,minLengthTree=0;
vector<TreeNode*> path_tree,result_pPath,result_qPath;
preOrder(root,p,path_tree,result_pPath,finish);
if(finish==1)
{
finish=0;
path_tree.clear();
}
preOrder(root,q,path_tree,result_qPath,finish);
if(result_pPath.size()<result_qPath.size())
{
minLengthTree=result_pPath.size();
}else
{
minLengthTree=result_qPath.size();
}
TreeNode* result=0;
for(int i=0;i<minLengthTree;++i)
{
if(result_pPath[i]==result_qPath[i])
{
result=result_pPath[i];
}
}
return result;
}
};
解法2:
求深度最深以及找祖先或找路径这种问题,优先考虑后序遍历。
分析条件:满足结果分俩种情况
1.当前节点为所需求的p或q且左子树或右子树找到另外一个节点。
2.当前节点的左子树或右子树分别找到俩个节点。(ps:因为后序,所以深度是最深,所以合理应用后序遍历是很好的,详情参考王道数据结构二叉树相关选择题)
/**
* 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:
bool postOrder(TreeNode* root, TreeNode* p, TreeNode* q,TreeNode* &result,bool fLson,bool fRson,bool &findFather)
{
if(root==nullptr||findFather) return false;
fLson=postOrder(root->left,p,q,result,fLson,fRson,findFather);
fRson=postOrder(root->right,p,q,result,fLson,fRson,findFather);
if((fLson&&fRson)||(root==p||root==q)&&(fLson||fRson)) //&&>||
{
findFather=true;
result=root;
}
/* if(root==p||root==q)
{
return true;
}
*/ //此处学一下官方题解,它的语法糖更优。
return fLson||fRson||root==p||root==q;
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
TreeNode* result=0;
bool findFather=false;//优化时间
postOrder(root,p,q,result,false,false,findFather);
return result;
}
};
解法三:
使用um_Father哈希记录每个节点的父亲节点。
使用um_IfFindNode哈希记录找到p或q后该路径所有的节点的visit为true
若先找到p,p后该路径(从p到根)所有的节点的visit为true
再找到q时,若遍历该路径时发现其中一个节点的visit为true时候,即该节点为最近公共祖先。
/**
* 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:
void postOrder(TreeNode* root,TreeNode* p,TreeNode* q,unordered_map<TreeNode*,TreeNode*> &um_Father,unordered_map<TreeNode*,bool> &um_IfFindNode,TreeNode* &result,bool &isFinish)
{
if(root==nullptr||isFinish) return;
if(root->left&&um_Father.find(root->left)==um_Father.end())//如果找不到
{
um_Father[root->left]=root;
}
if(root->right&&um_Father.find(root->right)==um_Father.end())//如果找不到
{
um_Father[root->right]=root;
}
if(root==p||root==q)
{
um_IfFindNode[root]=true;
TreeNode* t=root;
while(um_Father.find(t)!=um_Father.end())
{
if(um_IfFindNode[um_Father[t]]==true)
{
result=um_Father[t];
isFinish=true;
return;
}
um_IfFindNode[um_Father[t]]=true;
t=um_Father[t];
}
}
postOrder(root->left,p,q,um_Father,um_IfFindNode,result,isFinish);
postOrder(root->right,p,q,um_Father,um_IfFindNode,result,isFinish);
return;
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
unordered_map<TreeNode*,TreeNode*> um_Father;
unordered_map<TreeNode*,bool> um_IfFindNode;
TreeNode* result=0;
bool isFinish=false;
postOrder(root,p,q,um_Father,um_IfFindNode,result,isFinish);
//unordered_map<TreeNode*,TreeNode*>::interator m_i;
/*cout<<"root ";
for(unordered_map<TreeNode*,TreeNode*>::iterator m_i=um_Father.begin();m_i!=um_Father.end();++m_i)
{
cout<<"root "<<m_i->first->val<<"root->father "<<m_i->second->val<<endl;
}*/
return result;
}
};