https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
Given the following binary tree: root = [3,5,1,6,2,0,8,null,null,7,4]
Example 1:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 Output: 3 Explanation: The LCA of nodes 5 and 1 is 3.Example 2:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 Output: 5 Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.Note:
- All of the nodes' values will be unique.
- p and q are different and both values will exist in the binary tree.
后序遍历+并查集,逻辑上没有难度,就是后序遍历的迭代实现比较重要
/**
* 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) {
map<TreeNode*, int> M; //维护树结点的编号
vector<int> disjoint; //并查集
stack<TreeNode*> S; //后续遍历栈
int cnt = 0;
bool p_get = false, q_get = false;
TreeNode *cur = root;
S.push(cur);
while(!S.empty()){
if(S.top()->left != cur && S.top()->right != cur){ //进入兄弟节点
gotoHLVFL(S);
}
//访问节点
cur = S.top(); S.pop();
M[cur] = cnt;
++cnt;
disjoint.push_back(-1);
if(cur->left != NULL) disjoint[M[cur->left]] = cnt-1;
if(cur->right != NULL) disjoint[M[cur->right]] = cnt-1;
if(cur == p) p_get = true;
if(cur == q) q_get = true;
if(p_get && q_get && finish(disjoint, M[p], M[q])) return cur;
}
return root;
}
int find_in_disjoint(vector<int> &disjoint, int cn){
if(disjoint[cn] == -1) return cn;
return (disjoint[cn] = find_in_disjoint(disjoint, disjoint[cn]));
}
bool finish(vector<int> &disjoint, int cn1, int cn2){
cn1 = find_in_disjoint(disjoint, cn1);
cn2 = find_in_disjoint(disjoint, cn2);
return cn1 == cn2;
}
void gotoHLVFL(stack<TreeNode*> &S){
TreeNode *cur;
while((cur = S.top()) != NULL){
if(cur->left != NULL){
if(cur->right != NULL) S.push(cur->right);
S.push(cur->left);
// cur = cur->left;
}else{
S.push(cur->right);
// cur = cur->right;
}
}
S.pop();
}
};
除了这种针对最近祖先问题的传统解法之外,还看到了别人的更巧妙(?)的做法,使用了递归方法,并利用引用来实现状态的向上传递
/**
* 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* helper(TreeNode* root, TreeNode* p, TreeNode* q, int& pcheck, int& qcheck) {
if (root==NULL)
return root;
//post
int pc=0, qc=0; //self, hope check by the sons.
TreeNode* leftRes = helper(root->left, p, q, pc, qc);
if (leftRes!=NULL)
return leftRes;
TreeNode* rightRes = helper(root->right, p, q, pc, qc);
if (rightRes!=NULL)
return rightRes;
if (p==root)
pc = 1;
if (q==root)
qc = 1;
if (pc)
pcheck = 1;
if (qc)
qcheck = 1;
if (pc && qc)
return root;
return NULL;
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (root==NULL)
return root;
int pc=0,qc=0;
return helper(root, p, q, pc, qc);
}
};
注意 pcheck和qcheck是用于向上传递的参数,他们的值针对每一次的调用并没有作用(即使他们的值是1,那也是祖先节点处获得的性质(某个祖先节点覆盖了这个p或q),对当前节点为根的子树没有影响),只有pc和qc代表了当前子树的性质