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.
方法一:沿用236的方法,如果该节点为p或q,说明该节点为所求的LCA,否则在其左子树和右子树寻找他们的LCA,如果左右子树都找到LCA,只有可能该节点就是LCA,否则,一定有一个子树求到的LCA为空,而不为空的那个子树的LCA即为我们所求结果。
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root == NULL)
return root;
if(root == p || root == q)
return root;
TreeNode* left = lowestCommonAncestor(root->left, p, q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);
if(left != NULL && right != NULL)
return root;
else if(left != NULL)
return left;
else if(right != NULL)
return right;
return NULL;
}
};
解题思路:先遍历找到两个节点从根节点到该节点的路径,用0、1表示走左子树还是右子树。然后再寻找两个路径第一个不一样的分叉。
class Solution {
public:
int flag = 0;
bool findflag = false;
vector<int>temppath[2],anspath[2];
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
findvalue(root,p);
flag = 1;
findflag = false;
findvalue(root,q);
TreeNode *ans = root;
int cnt = 0, len = min(anspath[0].size(), anspath[1].size());
while(cnt < len && anspath[0][cnt] == anspath[1][cnt]){
if(anspath[0][cnt] == 0)
ans = ans->left;
else
ans = ans->right;
cnt++;
}
return ans;
}
void findvalue(TreeNode *root, TreeNode *x){
if(root == NULL || findflag == true)
return;
if(root->val == x->val){
anspath[flag] = temppath[flag];
findflag = true;
return;
}
temppath[flag].push_back(0);
findvalue(root->left, x);
temppath[flag].pop_back();
temppath[flag].push_back(1);
findvalue(root->right, x);
temppath[flag].pop_back();
}
};