Leetcode#236. 二叉树的最近公共祖先

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) 
    {
        if(root == NULL || root == p || root == q)
            return root;
        TreeNode* l = lowestCommonAncestor(root->left, p, q);
        TreeNode* r = lowestCommonAncestor(root->right, p, q);
        if(l && r)
            return root;
        if(l)
            return l;
        else
            return r;
    }
};

你可能感兴趣的:(========算,法=========,二叉树,======,Leetcode,=======)