题目:
代码(首刷看解析 10.1):
class Solution {
public:
TreeNode* ans=nullptr;
bool FindSon(TreeNode* root,TreeNode* p,TreeNode* q){
if(root == nullptr) return false;
bool lson = FindSon(root->left,p,q);
bool rson = FindSon(root->right,p,q);
if( (lson&&rson) || ((root->val==p->val)||(root->val==q->val)) && (lson||rson) ){
ans=root;
}
return lson||rson||((root->val==p->val)||(root->val==q->val));
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
FindSon(root,p,q);
return ans;
}
};
后续多做几遍