题目链接:https://www.nowcoder.com/practice/e0cc33a83afe4530bcec46eba3325116?tpId=196&rp=1&ru=%2Fexam%2Foj&qru=%2Fexam%2Foj&sourceUrl=%2Fexam%2Foj%3Fpage%3D2%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D196&difficulty=&judgeStatus=&tags=&title=102&gioEnter=menu
题目如下:
class Solution {
public:
int lowestCommonAncestor(TreeNode* root, int o1, int o2) {
return dfs(root,o1,o2)->val;
}
TreeNode* dfs(TreeNode* root,int val1,int val2){
if(!root||root->val==val1||root->val==val2) return root;
auto left=dfs(root->left,val1,val2);
auto right=dfs(root->right,val1,val2);
if(left&&right) return root;
return left!=nullptr?left : right;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31