题目链接:https://www.nowcoder.com/practice/e0cc33a83afe4530bcec46eba3325116?tpId=295&tqId=1024325&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj
思路:
代码:
int lowestCommonAncestor(struct TreeNode* root, int o1, int o2 )
{
if(root==NULL)
{
return -1;
}
if(root->val==o1||root->val==o2)
{
return root->val;
}
int left=lowestCommonAncestor(root->left,o1,o2);
int right=lowestCommonAncestor(root->right,o1,o2);
if(right==-1)
{
return left;
}
if(left==-1)
{
return right;
}
return root->val;
}