Created
June 15, 2020 02:40
-
-
Save shubham1710/9ac1a3bc17c20af6f766b0d9210acaf3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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