TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if (root == NULL) return NULL; if (p == NULL || q == NULL) return p == NULL ? q : p; if (root->val > p->val && root->val > q->val) return lowestCommonAncestor(root->left, p, q); if (root->val < p->val && root->val < q->val) return lowestCommonAncestor(root->right, p, q); return root; }
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { // return value // null: not found // non-null: having a represented node // case 1: root is null, not found. if(!root){ return nullptr; } // case 2: p/q found, propagate the p or q if(root == p || root == q){ return root; } TreeNode* left_side = lowestCommonAncestor(root->left, p, q); TreeNode* right_side = lowestCommonAncestor(root->right, p, q); // case 3: if left_side and right_side are both found, the none p/q lowestCommonAncestor found. if(left_side && right_side){ return root; } // case 4: if only left_side found, propagate the result(case 2 and 3) if(left_side) { return left_side; } // case 5: if only right_side found, propagate the result(case 2 and 3) if(right_side) { return right_side; } // case 6: nothing found return nullptr; }
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if (!root ||p==root ||q==root) return root; int match = countMatch(root->left, p, q); if (match==1) return root; else if (match==2) lowestCommonAncestor(root->left, p, q); else lowestCommonAncestor(root->right, p, q); }
// Recursive TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(p->val < root->val && q->val < root->val) return lowestCommonAncestor(root->left, p, q); if(p->val > root->val && q->val > root->val) return lowestCommonAncestor(root->right, p, q); return root; }
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(root == p || root == q) return root; if(p->val > q->val) swap(p, q); if (root->val > p->val && root->val < q->val) return root; if (root->val > p->val && root->val > q->val) return lowestCommonAncestor(root->left, p, q); if (root->val < p->val && root->val < q->val) return lowestCommonAncestor(root->right, p, q); }
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { // swap p and q if p has a larger val. if (p->val > q->val) { TreeNode* swap = p; p = q; q = swap; } bool p_isLeft = isUnder(root->left, p); bool q_isRight = isUnder(root->right, q); bool p_isRight = isUnder(root->right, p); bool q_isLeft = isUnder(root->left, q); // Is p left or myself? // Is q right or myself? // If both yes, then I am the LCA; if ( (p_isLeft || (p == root)) && (q_isRight || (q == root)) ) return root; // Are both p and q to the left? Then the LCA is somewhere under my left node. if ( p_isLeft && q_isLeft ) return lowestCommonAncestor(root->left, p, q); // Are both p and q to the right? Then the LCA is somewhere under my right node. if ( p_isRight && q_isRight ) return lowestCommonAncestor(root->right, p, q); }
void lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q, bool* pFound, bool* qFound, TreeNode** solution) { bool lpFound = false; bool lqFound = false; bool rpFound = false; bool rqFound = false; if (root == NULL) { return; } lowestCommonAncestor(root->left, p, q, &lpFound, &lqFound, solution); if (*solution != NULL) { return; } lowestCommonAncestor(root->right, p, q, &rpFound, &rqFound, solution); if (*solution != NULL) { return; } *pFound = lpFound || rpFound || (root == p); *qFound = lqFound || rqFound || (root == q); if (*pFound && *qFound) { *solution = root; } }
TreeNode * lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if (!root) return NULL; if ((root->val > p->val && root->val < q->val) || (root->val < p->val && root->val > q->val)) return root; if (root->val == p->val || root->val == q->val) return root; if (root->val > p->val && root->val > q->val) return lowestCommonAncestor(root->left, p, q); if (root->val < p->val && root->val < q->val) return lowestCommonAncestor(root->right, p, q); }
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if (!root) { return nullptr; } if (root == p || root == q) { return root; } auto l = lowestCommonAncestor(root->left, p, q), r = lowestCommonAncestor(root->right, p, q); return l && r ? root : l ? l : r; }
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) { if (root == NULL || root == p || root == q) return root; struct TreeNode* l = lowestCommonAncestor(root->left, p, q); struct TreeNode* r = lowestCommonAncestor(root->right, p, q); if (l && r) return root; return l ? l : r; }
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(root == NULL) { return NULL; } if(root == p || root == q) { return root; } // Divide TreeNode* left = lowestCommonAncestor(root->left, p, q); TreeNode* right = lowestCommonAncestor(root->right, p, q); // Conquer // if one side return NULL, that means p and q may at the other side and its return value may LCA // if both side are NULL, return NULL (either side) if(left == NULL) { return right; } else if (right == NULL) { return left; // if both sides are not return NULL, root must be LCA } else { return root; } }
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if (p->val <= root->val && q->val >= root->val || p->val >= root->val && q->val <= root->val) return root; if (root->val > max(p->val, q->val)) return lowestCommonAncestor(root->left, p, q); else return lowestCommonAncestor(root->right, p, q); }
int main(){ struct TreeNode* root = malloc(sizeof(struct TreeNode)); root->val = 6; struct TreeNode* t1 = malloc(sizeof(struct TreeNode)); t1->val = 2; struct TreeNode* t2 = malloc(sizeof(struct TreeNode)); t2->val = 8; struct TreeNode* t3 = malloc(sizeof(struct TreeNode)); t3->val = 0; struct TreeNode* t4 = malloc(sizeof(struct TreeNode)); t4->val = 4; struct TreeNode* t5 = malloc(sizeof(struct TreeNode)); t5->val = 7; struct TreeNode* t6 = malloc(sizeof(struct TreeNode)); t6->val = 9; struct TreeNode* t7 = malloc(sizeof(struct TreeNode)); t7->val = 3; struct TreeNode* t8 = malloc(sizeof(struct TreeNode)); t8->val = 5; root->left = t1; root->right = t2; t1->left = t3; t1->right = t4; t2->left = t5; t2->right = t6; t4->left = t7; t4->right = t8; printf("%p, %p\n", root, lowestCommonAncestor(root, t1, t2)); printf("%p, %p\n", t1, lowestCommonAncestor(root, t1, t4)); printf("%p, %p\n", t1, lowestCommonAncestor(root, t4, t1)); printf("%p, %p\n", root, lowestCommonAncestor(root, t3, t6)); printf("%p, %p\n", t2, lowestCommonAncestor(root, t5, t6)); }
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if (!root || p == root || q == root) return root; TreeNode *left = lowestCommonAncestor(root->left, p, q); TreeNode *right = lowestCommonAncestor(root->right, p , q); if (left && right) return root; return left ? left : right; }
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(!root || root == p || root == q) return root; TreeNode* left = lowestCommonAncestor(root->left, p, q); TreeNode* right = lowestCommonAncestor(root->right, p, q); return left && right ? root : left ? left : right; }
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if (!root || root == p || root == q) return root; TreeNode* left = lowestCommonAncestor(root->left, p, q); TreeNode* right = lowestCommonAncestor(root->right, p, q); if (!left) return right; if (!right) return left; return root; }
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(root == NULL) return NULL; TreeNode* f = p->val < q->val ? p:q ; TreeNode* l = p->val > q->val ? p:q ; if(root->val >= f->val && root->val <= l->val) return root; else if(root->val > l->val && root->left != NULL) lowestCommonAncestor( root->left, f,l); else if(root->val < f->val && root->right != NULL) lowestCommonAncestor(root->right, f,l); }
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) { if(is_ancestor(root->left,p) && is_ancestor(root->left,q)) return lowestCommonAncestor(root->left,p,q); if(is_ancestor(root->right,p) && is_ancestor(root->right,q)) return lowestCommonAncestor(root->right,p,q); return root; }
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(!root || root==p || root==q) return root; auto Left = lowestCommonAncestor(root->left,p,q); auto Right = lowestCommonAncestor(root->right,p,q); if(Left&&Right) return root; else if(Left) return Left; else return Right; }
/** * 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->val > p->val && root->val > q->val) return lowestCommonAncestor(root->left,p,q); if(root->val < p->val && root->val < q->val) return lowestCommonAncestor(root->right,p,q); return root; }
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(root->val > p->val && root->val > q->val) return lowestCommonAncestor(root->left,p,q); else if(root->val < p->val && root->val < q->val) return lowestCommonAncestor(root->right,p,q); else return root; }
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(root){ if( (p->val - root->val)*(q->val - root->val)<=0 ) return root; else if (p->val < root->val) return lowestCommonAncestor(root->left, p, q); else return lowestCommonAncestor(root->right, p, q); } }
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(p == root || q == root || root == NULL) return root; TreeNode *left = lowestCommonAncestor(root->left, p, q); TreeNode *right = lowestCommonAncestor(root->right, p, q); if(left && right) // p和q分别落于root的左右两个subtree中 return root; return left ? left : right; }
TreeNode* Solution84::lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(root == NULL || p == NULL || q == NULL) return NULL; if(root->val<=p->val && root->val>=q->val || root->val>=p->val && root->val<=q->val) return root; if(root->val<=p->val && root->val<=q->val) return lowestCommonAncestor(root->right, p, q); return lowestCommonAncestor(root->left, p, q); }
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(root==NULL || p==NULL || q==NULL) return NULL; if((p->val< root->val) && (q->val < root->val)) { return lowestCommonAncestor(root->left, p, q); } else if((p->val> root->val)&& (q->val > root->val)) { return lowestCommonAncestor(root->right, p, q); } else return root; }
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if (!root || !p || !q) return NULL; if (root == p || root == q) return root; TreeNode *leftLCA = lowestCommonAncestor(root->left, p, q); TreeNode *rightLCA = lowestCommonAncestor(root->right, p, q); if (leftLCA && rightLCA) return root; return leftLCA ? leftLCA : rightLCA; }
//AC - 16ms; struct TreeNode* lowestCommonAncestor0( struct TreeNode* root, struct TreeNode *p, struct TreeNode* q ) { if(!root || root == p || root == q) return root; struct TreeNode *left = lowestCommonAncestor(root->left, p, q); struct TreeNode *right = lowestCommonAncestor(root->right, p, q); if(left&&right) return root; if(!left) return right; if(!right) return left; }
//·¨¶þ TreeNode* lowestCommonAncestor2(TreeNode* root, TreeNode* p, TreeNode* q) { if(root==nullptr||p==root||q==root) return root; TreeNode* left=lowestCommonAncestor(root->left,p,q); TreeNode* right=lowestCommonAncestor(root->right,p,q); if(left&&right) return root; if(left&&!right) return left; if(!left&&right) return right; return nullptr; }
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if (cover(root->left, p) && cover(root->left, q)) { return lowestCommonAncestor(root->left, p, q); } if (cover(root->right, p) && cover(root->right, q)) { return lowestCommonAncestor(root->right, p, q); } return root; }
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if ((root->val > p->val) && (root->val > q->val)) { return lowestCommonAncestor(root->left, p, q); } else if((root->val < p->val) && (root->val < q->val)){ return lowestCommonAncestor(root->right, p, q); } else { return root; } return 0; }