二叉树的最近公共祖先

TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *A, TreeNode *B) {
        if(!root)
            return NULL;
        if(root==A)
            return A;
        if(root==B)
            return B;
        TreeNode *L=lowestCommonAncestor(root->left,A,B);
        TreeNode *R=lowestCommonAncestor(root->right,A,B);
    
        if(L&&R)
            return root;
            
        return L?L:R;    
    }

你可能感兴趣的:(二叉树的最近公共祖先)