1.p q不互为子孙,一个在fa的左,一个在fa的右【返回fa】
2.p q互为子孙,p是q的孙或q是p的孙【返回第一个找到的p/q]
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(!root || root==p || root==q) return root;
TreeNode* l=lowestCommonAncestor(root->left,p,q),*r=lowestCommonAncestor(root->right,p,q);
if(l && r) return root;
else return l? l:r;
}
};