Skip to content

Instantly share code, notes, and snippets.

@shubham1710
Created June 15, 2020 02:40
Show Gist options
  • Save shubham1710/9ac1a3bc17c20af6f766b0d9210acaf3 to your computer and use it in GitHub Desktop.
Save shubham1710/9ac1a3bc17c20af6f766b0d9210acaf3 to your computer and use it in GitHub Desktop.
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if( root == p || root == q || root == NULL)
return root;
TreeNode * parent1 = lowestCommonAncestor(root->left, p, q);
TreeNode * parent2 = lowestCommonAncestor(root->right, p, q);
if( parent1 && parent2)
return root;
else
return parent1 ? parent1:parent2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment