Skip to content

Commit

Permalink
Time: 17 ms (28.00%), Space: 14.4 MB (35.17%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed Oct 3, 2023
1 parent 33538d7 commit 985d736
Showing 1 changed file with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* ans;
bool flag=true;
bool chk(TreeNode* root, TreeNode* qu)
{
if(root==qu)return true;
if(root==NULL)return false;
bool a=chk(root->left,qu);
bool b= chk(root->right, qu);
return a || b;
}

bool solve(TreeNode* root, TreeNode* p,TreeNode* q)
{
if(root==NULL)return false;

if(root==p)
{
if(chk(root,q))
{
ans=root;

return true;
}
else
return true;
}
else if(root==q)
{
if(chk(root,p))
{
ans=root;

return true;
}
else
return true;
}
bool a=solve(root->left,p,q);

bool b=solve(root->right,p,q);
if(a==true && b==true && flag )
{
ans=root;
flag=false;
}
return a||b;
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {

solve(root, p,q);

return ans;

}
};

0 comments on commit 985d736

Please sign in to comment.