rav16783

Untitled

Feb 10th, 2023
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. class Solution {
  2. public:
  3. TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* n1, TreeNode* n2) {
  4. if(root == NULL) return NULL;
  5. if((root->val == n1->val)|| (root->val == n2->val)){
  6. return root;
  7. }
  8. TreeNode* leftans = lowestCommonAncestor(root->left,n1,n2);
  9. TreeNode* rightans = lowestCommonAncestor(root->right,n1,n2);
  10. if(leftans != NULL && rightans != NULL ) return root;
  11. else if(leftans != NULL && rightans ==NULL) return leftans;
  12. else if(leftans == NULL && rightans !=NULL) return rightans;
  13. else return NULL;
  14. }
  15. };
Advertisement
Add Comment
Please, Sign In to add comment