Leetcode 236
/**
* 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 has found the acsentor -> returning the root**
if(root == nullptr || root == p || root == q)
return root;
**//going down to continue serach**
TreeNode* left = lowestCommonAncestor(root -> left, p, q);
TreeNode* right = lowestCommonAncestor(root -> right, p, q);
**//if left is found -> return left else return right 反之亦返**
if(left != NULL && right != NULL){
return root;
}
return left == NULL ? right : left;//当前最近的一直回溯
}
};