Lowest Common Ancestor in Binary Tree | C++ Placement Course | Lecture 27.15

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ก.ย. 2024
  • Complete C++ Placement Course (Data Structures+Algorithm) : • C++ Full Course | C++...
    Telegram: t.me/apnikaksh...
    Instagram: / dhattarwalaman
    Notes of this Lecture:

ความคิดเห็น • 104

  • @SurajMapariPrivate
    @SurajMapariPrivate 3 ปีที่แล้ว +22

    This Man Is Going Crazy Keep It Up 😆 Thanks You So Much For All The Lectures❤️🙏

  • @vedantshinde2277
    @vedantshinde2277 3 ปีที่แล้ว +11

    You are literally doing the lord's work!!

  • @nitishtellakula7243
    @nitishtellakula7243 3 ปีที่แล้ว +3

    in the lac2 , at " 3 " root->next will be checked and after failing in "or" condition next step ie., left and right trees are called and they return 7,6 are present ,hence in the present 3 function in recurssion stack both leftlca(7) and rightlca(6) got these values so they aren't NULL and as we are present at root whose data is 3(root->data) so return "3" takes place :)

  • @ritiksharma1229
    @ritiksharma1229 2 ปีที่แล้ว +2

    after introduction with trees i really thought of solving govt. exam ques like what is the relation between a and b? and here it is the example of family tree... great lectures!!!!

  • @shivambhasin1308
    @shivambhasin1308 3 ปีที่แล้ว +5

    love this course man but just wanted to inform that this lecture should be above the flattening of binary tree vid

  • @hardiknegi7066
    @hardiknegi7066 3 ปีที่แล้ว +6

    Mudde ki baat to end k 5 min me hui h😂💯
    Btw notes are much needed now 🙏🏻

  • @ayushgoel4145
    @ayushgoel4145 2 ปีที่แล้ว +1

    This course is perfect for students who want to visualize the concept, keep in mind to solve questions after watching these videos!

  • @mohammedimtiyaz2410
    @mohammedimtiyaz2410 3 ปีที่แล้ว +7

    Optimized approach is easier than the general one :)

    • @sudeepbhagat5027
      @sudeepbhagat5027 3 ปีที่แล้ว

      Hlo bro can you tell me where is the notes for all this vedio
      In the description there is no link for this lecture . Can you please solve my issue ?

    • @mohammedimtiyaz2410
      @mohammedimtiyaz2410 3 ปีที่แล้ว +2

      @@sudeepbhagat5027 donno man
      my suggestion is write your own notes, that'll be helpful :)

    • @sudeepbhagat5027
      @sudeepbhagat5027 3 ปีที่แล้ว

      Ok no problem i am creating my own notes but notes which provides by faculty is contained some more question so that why I am asking.

  • @shaileshhacker
    @shaileshhacker 2 ปีที่แล้ว +4

    9:11 for loop condition should be:
    pc

    • @dhruvsakariya3129
      @dhruvsakariya3129 ปีที่แล้ว +2

      I wasn't know thank you for letting me know 🙏🏽🙏🏽

  • @samarthbhawsar8739
    @samarthbhawsar8739 3 ปีที่แล้ว +3

    Great Explanation!🙌🙌
    Keep it up✔

  • @ash_79
    @ash_79 3 ปีที่แล้ว +3

    @Apna College In the 2nd optimal code there will be a major mistake that it doesn't handle. The code is looking for either n1 or n2 value in the left or right subtree so it can get one value in both right and left subtree and will return some ans, which is false.

    • @sit33darshanpagar16
      @sit33darshanpagar16 2 ปีที่แล้ว

      No it is look n1 or n2 in own data , which is far more different than you speaking

    • @KrishanKumar-hp5yu
      @KrishanKumar-hp5yu 2 ปีที่แล้ว +1

      Your case will happen only if same value is repeated in both side of a binary tree

  • @paragsaini2795
    @paragsaini2795 3 ปีที่แล้ว +2

    In the LCA2 before running LCA2 we must ensure that n1 and n2 are present in the tree. Otherwise it will give wrong answer.
    For example if tree is 1->2->3 and n1 = 1 and n2 = 4. It give lca as '1' which is wrong.

    • @anuragpandey3341
      @anuragpandey3341 3 ปีที่แล้ว

      It will automatically be handled by base case where we reach root == NULL and it will return NULL

    • @paragsaini2795
      @paragsaini2795 3 ปีที่แล้ว +1

      @@anuragpandey3341 no, on the first call itself, root-> data == 1, which then return root itself. Gives answer as 1

    • @anuragpandey3341
      @anuragpandey3341 3 ปีที่แล้ว

      Oh, yes, correct.......

    • @sonalidutta825
      @sonalidutta825 2 ปีที่แล้ว

      @@paragsaini2795 last m condition lga skte h if ans==n1 /n2 return NULL

    • @sit33darshanpagar16
      @sit33darshanpagar16 2 ปีที่แล้ว

      @@sonalidutta825 oh ya

  • @mandeep2192
    @mandeep2192 3 ปีที่แล้ว

    this was THEEEE bESST xplaination i've seeeen so faar on youtube.....thnxxx

  • @artyCrafty4564
    @artyCrafty4564 ปีที่แล้ว

    May Allah bless you @ApnaCollegeOfficial..👍🤗🤗🤗thanx for such valuable content

  • @agarwalji1109
    @agarwalji1109 3 ปีที่แล้ว +11

    Neha toh rahul ki girlfriend thi pehle... Ab behen😂 . Ghor kalyug😂😂😂

  • @saurabhkumar5941
    @saurabhkumar5941 3 ปีที่แล้ว +1

    In 2nd approach,
    if both n1 & n2 does not exist in tree => it returns NULL node.
    if either n1 or n2 does not exist in the tree => it returns the existing node (it does not return NULL node)

    • @prathamnarwade1637
      @prathamnarwade1637 3 ปีที่แล้ว

      exactly if we take n1 as 7 and n2 as 8... as 7 is in the tree but 8 is not in the tree it should give output as null but its showing as 7.....plzz can someone explain this..

    • @gauravupreti9340
      @gauravupreti9340 3 ปีที่แล้ว

      @@prathamnarwade1637 Actually they forgot to add a constraint that either both nodes are present or no one is present. In gfg they mention it clearly.

  • @RajanGupta-fj8fi
    @RajanGupta-fj8fi 2 ปีที่แล้ว +1

    Is video ko shortest node ke pehle place karna chaiye tha didi

  • @harshpanwar1550
    @harshpanwar1550 2 ปีที่แล้ว

    Such a valuable lecture🤗🤗.
    Thank u didi...

  • @justanotheraspiringsinger8335
    @justanotheraspiringsinger8335 ปีที่แล้ว

    All videos are awesome

  • @EverythingaboutTechPro
    @EverythingaboutTechPro 3 ปีที่แล้ว +6

    Code is incorrect for LCA2 ..

    • @saumyatiwari4030
      @saumyatiwari4030 3 ปีที่แล้ว +1

      How?

    • @himanshuraj5689
      @himanshuraj5689 3 ปีที่แล้ว

      @@saumyatiwari4030 before returning root in || operator function...code must tell that both n1 and n2 is present in the tree..

  • @adarshgautam4136
    @adarshgautam4136 3 ปีที่แล้ว

    Thank You Ma'am !! for your wonderful Explanantion.

  • @praveenanand5689
    @praveenanand5689 2 ปีที่แล้ว +1

    notes kha milenge binary tree ke

  • @HimanshuSingh-zu4ht
    @HimanshuSingh-zu4ht 3 ปีที่แล้ว +6

    In lca2 , If one key is present and the other is absent, then it returns the present key as LCA (Ideally should have returned NULL)
    so please make another video on lca2(new version)
    so that it will handle all cases

    • @ARYANSHARMA-ph8ss
      @ARYANSHARMA-ph8ss 3 ปีที่แล้ว

      Can you elaborate. I am not able to get what you were trying to say. If you can, can explain with examples.

    • @KADOfficial23
      @KADOfficial23 3 ปีที่แล้ว +2

      @@ARYANSHARMA-ph8ss actually second approach has a precondition that "Nodes are present" as an assumption... but she didn't tell in the video about it

    • @ARYANSHARMA-ph8ss
      @ARYANSHARMA-ph8ss 3 ปีที่แล้ว

      @@KADOfficial23 han bro abhi leetcode pe question kara toh second test case main hi phas gya lol.

    • @ARYANSHARMA-ph8ss
      @ARYANSHARMA-ph8ss 3 ปีที่แล้ว

      @@KADOfficial23 we can avoid it tho by adding if(path1.at(i)==path2.at(i) && (i+1==path1.size() || i+1==path2.size())) return path1.at(i);

    • @jaikumarsingh4030
      @jaikumarsingh4030 3 ปีที่แล้ว +1

      you can use a "count" variable for that.....which will increment when it finds nodes and if count==2 then u can print LCA or else null

  • @youtubekumar8590
    @youtubekumar8590 ปีที่แล้ว

    Thanku Didi

  • @abhinavshukla6418
    @abhinavshukla6418 3 ปีที่แล้ว +1

    1 thing for sure for learnning trees u goota be strong with recursion nd backtracking.

  • @gauravupreti9340
    @gauravupreti9340 3 ปีที่แล้ว +1

    Another way to solve this problem
    #include
    #include
    using namespace std;
    class node
    {
    public:
    int data;
    node *left;
    node *right;
    node(int v)
    {
    data = v;
    left = NULL;
    right = NULL;
    pair ancestor(node *curr, int &x, int y)
    {
    if (curr == NULL)
    {
    pair p(false, 0);
    return p;
    }
    pair a = ancestor(curr->left, x, y);
    pair b = ancestor(curr->right, x, y);
    if (x == -1)
    {
    pair p(false, 0);
    return p;
    }
    if (curr->data == x )
    {
    if (a.first == true || b.first == true)
    {
    coutright->left = new node(6);
    root->right->right = new node(7);

    int a = 2, b =5;
    ancestor(root, a, b);
    return 0;
    }

  • @Amitkumar-io1of
    @Amitkumar-io1of 3 ปีที่แล้ว +23

    Rahul and Neha are not cousins 🤣🤣🤣🤣🤣

  • @blackstargaming3678
    @blackstargaming3678 ปีที่แล้ว

    why we are taking node* lca2 not int lca ?

  • @atin_jagotra
    @atin_jagotra 3 ปีที่แล้ว +2

    the code of LCA we did in "the shortest distance between 2 nodes" will it not work here and why ???

    • @nikhileshbaghel
      @nikhileshbaghel 3 ปีที่แล้ว +1

      It will work just u have change and write -1 and root data

  • @mysteriousocean3206
    @mysteriousocean3206 3 ปีที่แล้ว

    fantastic explanation

  • @vikasmane8851
    @vikasmane8851 3 ปีที่แล้ว +2

    😀yahi consistency chahiye

  • @PramendraSharma
    @PramendraSharma ปีที่แล้ว +2

    #include
    #include
    using namespace std;
    class Node{
    public:
    int data;
    Node* left;
    Node* right;
    Node(int val){
    data=val;
    left=NULL;
    right=NULL;
    }
    };
    bool getPath(Node *root, int key, vector &path){
    if(root==NULL){
    return false;
    }
    path.push_back(root->data);
    if(root->data==key){
    return true;
    }
    if(getPath(root->left, key, path)||getPath(root->right, key, path)){
    return true;
    }
    path.pop_back();
    return false;
    }
    int LCA(Node* root, int n1, int n2){
    vector path1, path2;
    if(!getPath(root, n1, path1) || !getPath(root, n2, path2)){
    return -1;
    }
    int pc;
    for(pc=0; pcdata==n1 || root->data==n2){
    return root;
    }
    Node* leftLCA= LCA2(root->left, n2, n1);
    Node* rightLCA=LCA2(root->right, n2, n1);
    if(leftLCA && rightLCA){
    return root;
    }
    if(leftLCA!=NULL){
    return leftLCA;
    }
    return rightLCA;
    }
    int main(){
    Node* root=new Node(1);
    root->left=new Node(2);
    root->right=new Node(3);
    root->left->left=new Node(4);
    root->right->left=new Node(5);
    root->right->right=new Node(6);
    root->right->left->left=new Node(7);
    Node *lca=LCA2(root, 7, 6);
    cout

  • @hustlewithVaibhav
    @hustlewithVaibhav 3 ปีที่แล้ว +1

    Thank you 🙏

  • @rajiv-59
    @rajiv-59 2 ปีที่แล้ว +1

    rahul aur neha ka relation finally aaj samajh mein aaya hai 😄

  • @nikhilsoni1511
    @nikhilsoni1511 หลายเดือนก่อน

    do not go for this long answer, below is the most optimal code for finding lca
    // find the lca of given two nodes in binary tree using cpp
    #include
    using namespace std;
    class node{
    public:
    int data;
    node* left;
    node* right;
    node(int val){
    data=val;
    left=NULL;
    right=NULL;
    }
    };
    node* findlca(node* root,int n1,int n2){
    if(root==nullptr){
    return nullptr;
    }
    if(root->data==n1 || root->data==n2) return root;
    node *leftlca= findlca(root->left,n1,n2);
    node*rightlca= findlca(root->right,n1,n2);
    if(leftlca && rightlca){
    return root;
    }
    return (leftlca!=nullptr)?leftlca:rightlca;
    };
    int main(){
    node* root= new node(1);
    root->left= new node(2);
    root->right= new node(3);
    root->left->left=new node(4);
    root->right->left = new node(5);
    root->right->right= new node(6);
    root->right->left->left = new node(7);
    int n1=7;
    int n2=6;
    node* lca = findlca(root,n1,n2);
    if(lca!=nullptr){
    cout

  • @Zeno_Yoru
    @Zeno_Yoru 3 ปีที่แล้ว

    Thanks bhaiya🙏🙏🙏

  • @darshan_heart_touching_p5688
    @darshan_heart_touching_p5688 3 ปีที่แล้ว +1

    Sir best computers in 30000 rupees ek video bnaaye

  • @sachingamer2438
    @sachingamer2438 2 ปีที่แล้ว

    Why do you not provided the notes of the lectures??

  • @dharmeshvala3726
    @dharmeshvala3726 3 ปีที่แล้ว

    Why we return distance 1+dl

  • @laxmikantsaraswat6319
    @laxmikantsaraswat6319 2 ปีที่แล้ว

    Node *lca(Node *root, int v1,int v2) {

    if (root==NULL) {
    return NULL;

    }
    if (root->data>v1 && root->data>v2) {
    return lca(root->left, v1, v2);
    }
    if (root->datadataright, v1, v2) ;
    }

    return root;


    }
    this is esay one!!

  • @AmitYadav-kr6vf
    @AmitYadav-kr6vf 3 ปีที่แล้ว

    Thank you mam

  • @shivabansfore7510
    @shivabansfore7510 3 ปีที่แล้ว

    honestly saying i watch only your video and i don't like other guy who also makes video.

  • @ujjwalverma2870
    @ujjwalverma2870 2 ปีที่แล้ว +1

    Paap ghor Paap
    Rahul or neha ko cousin bana kar ghor paap kar diya tumne 🥲

  • @aishiksarkar8226
    @aishiksarkar8226 2 ปีที่แล้ว

    What if [1,2,3,null,null,4,4,6,5].....and n1=6 and n2=5 answer toh galat ayga i guess?

  • @anmolhanda2658
    @anmolhanda2658 2 ปีที่แล้ว

    Playlist bohot shi chlri thi..great job done...lekin trees pr aate hi saara naas maar diya....🤦‍♂️ bs code likh kr read kr rhe ho aur kuch nhi...

  • @Adityasharma-oe8zp
    @Adityasharma-oe8zp 3 ปีที่แล้ว +1

    Bhai yarr magar rahul to neha ko date kar raha tha backtracking ke time pe😂😂

  • @avinashgupta2308
    @avinashgupta2308 3 ปีที่แล้ว +4

    oops rahul neha are cousins

  • @siddharthjain7751
    @siddharthjain7751 3 ปีที่แล้ว +1

    for(pc = 0 ;pc < path2.size() && path1.size() ; pc++)
    this part.
    pc < path2.size() && path1.size();
    can anyone explain?

    • @HIMANSHUVERMA-wu1hc
      @HIMANSHUVERMA-wu1hc 3 ปีที่แล้ว +1

      we will go up to the minimum path(path1,path2),,,,kyuki uss se aage jayege to already 1 array khatm ho chuka to common milne ke to chance hi khtm ... islye apn 2 condition use kr rhe hai taki ek minimum path wale array ke khtm hote hi condition false ho jaye
      hope it will help you...

    • @dankaxon4230
      @dankaxon4230 3 ปีที่แล้ว

      @@HIMANSHUVERMA-wu1hc not true it will run to the value compared first I tried it out on c++ 14

    • @cenacr007
      @cenacr007 3 ปีที่แล้ว

      simple since && is used vlue of pc should be less than both at all times else it will fail.

    • @vaibhavchauhan1652
      @vaibhavchauhan1652 3 ปีที่แล้ว

      it is just that pc should be less than path2.size() and also path1.size() simple logic you are traversing to your vector till it's last element..

    • @themohitgupta
      @themohitgupta 2 ปีที่แล้ว

      It is [pc

  • @shubhamgoel3998
    @shubhamgoel3998 2 ปีที่แล้ว

    void path(node *root,int k,vector *v) {
    if (root==NULL) {
    return;
    }
    if (root->data==k) {
    v->push_back(root->data);
    return;
    }
    vector v1,v2;
    path(root->left,k,&v1);
    path(root->right,k,&v2);
    if (!v1.empty()) {
    *v = v1;
    v->push_back(root->data);
    }
    if (!v2.empty()) {
    *v = v2;
    v->push_back(root->data);
    }
    }
    path dhundne ke liye yeh method use kar sakte haikya
    int LCM(node *root,int k1,int k2) {
    vector p1,p2;
    path(root,k1,&p1);
    path(root,k2,&p2);
    reversevecto(&p1);
    reversevecto(&p2);
    int ans=-1,n=min(p1.size(),p2.size());
    for (int i=0; i

  • @Adityasharma-oe8zp
    @Adityasharma-oe8zp 3 ปีที่แล้ว

    Twist is....rahul's father's name is rahul ll and grandfather's is rahul l
    This is aman dhattarwal supremacy
    Every boy is rahul

  • @punitgarg4627
    @punitgarg4627 2 ปีที่แล้ว

    waise aman bhaiya neha rahul karte, yha dono ko bhai behen bna diya 😂😂🤣

  • @ayushpattnaik3977
    @ayushpattnaik3977 3 ปีที่แล้ว +1

    should have taught DFS properly first

  • @GauravGupta-ri9pv
    @GauravGupta-ri9pv 3 ปีที่แล้ว

    what will be the LCA of 1 and 7 PLS tell

  • @ParthivShah
    @ParthivShah 3 ปีที่แล้ว

    👍

  • @youtubekakiddokiddo8404
    @youtubekakiddokiddo8404 3 ปีที่แล้ว

    Just tell java course release date. At least tell if it's gonna air or not sir

    • @youtubekakiddokiddo8404
      @youtubekakiddokiddo8404 3 ปีที่แล้ว +1

      @@adityapai5147 like your mom pays to comment:)

    • @garv1177
      @garv1177 3 ปีที่แล้ว +1

      Bhai Apna Kaksha channel mein dekho Java with DS Algo ka ek pura playlist diya hua haa. dekhlo waha se ho jaega.

    • @youtubekakiddokiddo8404
      @youtubekakiddokiddo8404 3 ปีที่แล้ว

      @@garv1177 naya playlist aane wala tha bro toh socha with hand to hand wohi se kar lu

    • @garv1177
      @garv1177 3 ปีที่แล้ว +1

      @@youtubekakiddokiddo8404 time lagega Ane mein cp aur coding rounds clear karne mein c++ he madad karega Zyada. Naya Java Ane mein deri hoga. But as you wish Naya aya to acha

    • @youtubekakiddokiddo8404
      @youtubekakiddokiddo8404 3 ปีที่แล้ว

      @@garv1177 ha mai first year me hi hu abhi. I'll wait till this end sem

  • @anubhavrawat574
    @anubhavrawat574 3 ปีที่แล้ว +1

    Wrost Explaination Till Yet... Ratt ke baithe hai yeh ladki

  • @AdityaSharma8421
    @AdityaSharma8421 3 ปีที่แล้ว +6

    Rahul and neha cousins 😂

  • @mohdkaif6227
    @mohdkaif6227 3 ปีที่แล้ว

    Any One in last year of college??

  • @thatnaman
    @thatnaman 3 ปีที่แล้ว

    The family tree hierarchy example is sexist and patriarchal.

    • @anuragpandey3341
      @anuragpandey3341 3 ปีที่แล้ว +1

      Not the right place to discuss this....( *if this was not a sarcasm* )