/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
if(!root) return NULL;
if(root==p||root==q) return root;
struct TreeNode* left=lowestCommonAncestor(root->left,p,q);
struct TreeNode* right=lowestCommonAncestor(root->right,p,q);
if(left==p&&right==q||left==q&&right==p) return root;
if(left!=p&&left!=q && left) return left;
if(right&&right!=q&&right!=p) return right;
if(left==p||left==q) return left;
if(right==p||right==q) return right;
return NULL;
}
结果:
标签:right,TreeNode,struct,祖先,二叉树,return,236,root,left From: https://www.cnblogs.com/llllmz/p/18073747