/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类
* @param o1 int整型
* @param o2 int整型
* @return int整型
*/
TreeNode *DoseItExist(TreeNode *root,int x,int y){
if(root==nullptr){
return nullptr;
}
if(root->val==x||root->val==y){
return root;
}
TreeNode* left_exist=DoseItExist(root->left,x,y);
TreeNode* right_exist=DoseItExist(root->right,x,y);
if(left_exist&&right_exist){
return root;
}
else if(left_exist){
return left_exist;
}
else if(right_exist){
return right_exist;
}
else{
return nullptr;
}
}
int lowestCommonAncestor(TreeNode* root, int o1, int o2) {
return DoseItExist(root,o1,o2)->val;
}
};