Convert a Binary Tree into Doubly Linked List | Love Babbar DSA Sheet | Amazon | Microsoft 🔥 | GFG

Поділитися
Вставка
  • Опубліковано 8 лют 2025
  • #tree #competitiveprogramming #coding #dsa
    Hey Guys in this video I have explained with code how we can solve the problem 'Convert a Binary Tree into Doubly Linked List'.
    Array question Playlist = • Love Babbar DSA 450 Qu...
    String question Playlist = • Love Babbar DSA 450 Qu...
    Searching and Sorting question Playlist = • Love Babbar DSA 450 Qu...
    Binary Tree question Playlist = • Love Babbar DSA 450 Qu...
    Dynamic Programming question Playlist = • Love Babbar DSA 450 Qu...
    Roadmap for Dynamic Programming = • Complete Roadmap for D...
    Great Strategy to solve DSA = • Great Strategy to solv...
    My Journey to 5 star at codechef = • My Journey to 5 Star a...
    Love Babbar DSA Sheet : drive.google.c...
    Follow us on Instagram:
    Shailesh Yogendra : / shaileshyogendra
    Yogesh Yogendra : / i_am_yogesh_here
    Follow us on LinkedIn:
    Yogesh Yogendra : / yogesh-yogendra-26bbb518a
    Shailesh Yogendra : / shailesh-yogendra-8b13...
    Hope you like it. Comment if you have any doubt
    LIKE | SHARE | SUBSCRIBE

КОМЕНТАРІ • 61

  • @aditigupta3128
    @aditigupta3128 3 роки тому +48

    instead of cheking (f==0) for the first node, we can check (prev==null) becoz prev will only be null in case of first node. No need of taking f variable.

    • @pratibhaverma9946
      @pratibhaverma9946 3 роки тому +1

      we just want it enter if loop once without f it will enter for every node->left==null

    • @prabhatmishra5667
      @prabhatmishra5667 2 роки тому +3

      You are right ,here is the code
      void Solve(Node *root,Node *&head, Node*&prev)
      { if(root == nullptr) return;
      Solve(root->left,head,prev);
      if(!head) head = prev = root;
      else{prev->right = root;
      root->left = prev;
      prev = root; }
      Solve(root->right,head,prev);
      }
      Node * bToDLL(Node *root)
      { Node *head = NULL; Node *prev=NULL;
      Solve(root,head,prev);
      return head;
      }

  • @sumitjindal1115
    @sumitjindal1115 3 роки тому +22

    binary tree from strings....solve that question!!

  • @ani68
    @ani68 2 роки тому +2

    All these videos shows how hard he worked to Crack a company like Amazon

  • @ParasFtl
    @ParasFtl 2 роки тому

    Shailiesh and Yogesh, I wish you both all the success in the world and all the happniness for you and your family. God bless you.

  • @soumyadeepdas1536
    @soumyadeepdas1536 2 роки тому

    Really superb explanation, Thank you!!

  • @aasthagoyal5059
    @aasthagoyal5059 2 роки тому

    how we have connected last node right with head?

  • @harshitavyas7497
    @harshitavyas7497 2 роки тому

    what an amazing question!!!!

  • @harshalgarg1149
    @harshalgarg1149 3 роки тому

    Nice explanation.

  • @animallover2205
    @animallover2205 3 роки тому

    clear and crisp solution

  • @surajganguly7915
    @surajganguly7915 3 роки тому +1

    mujhe aaj pata laga ki shailesh brother and yogesh brother twins hai.

  • @ghazanferwahab5673
    @ghazanferwahab5673 3 роки тому +1

    My code in java
    class Solution
    {
    static void solve(Node root,Node head,Node prev){
    if(root==null)return;
    solve(root.left,head,prev);
    if(prev==null){
    head=root;
    prev=root;
    }
    else{
    prev.right=root;
    prev.right.left=prev;
    prev=prev.right;
    }
    solve(root.right,head,prev);
    }
    //Function to convert binary tree to doubly linked list and return it.
    Node bToDLL(Node root)
    {
    // Your code here
    Node head=null;
    Node prev=null;
    //int f=0;
    solve(root,head,prev);
    return head;
    }
    }

    • @ghazanferwahab5673
      @ghazanferwahab5673 3 роки тому

      @@risewithsakshi Mera bhi nai aya hai

    • @ranudrolia
      @ranudrolia 2 роки тому +1

      class Solution
      {
      static Node head;
      static Node prev;
      static void solve(Node root){
      if(root==null)return;
      solve(root.left);
      if(prev==null){
      head=root;
      prev=root;
      }
      else{
      prev.right=root;
      prev.right.left=prev;
      prev=root;
      }
      solve(root.right);
      }
      //Function to convert binary tree to doubly linked list and return it.
      Node bToDLL(Node root)
      {
      // Your code here
      head=null;
      prev=null;
      //int f=0;
      solve(root);
      return head;
      }
      }

  • @ghazanferwahab5673
    @ghazanferwahab5673 3 роки тому +1

    aree java mein run hi nai hora gfg ka bhi erro dera hai,sab test casse solve nai hora

  • @rohanrastogi1077
    @rohanrastogi1077 2 роки тому

    can we solve it iterartively?

  • @sanskarkumar7984
    @sanskarkumar7984 4 роки тому +1

    Finally 🤩🤩🤩. Jaldi jaldi Binary Trees and Bst upload kar digiye 🥰🥰

  • @virsr
    @virsr 2 роки тому +1

    This code not works in Java

  • @prashantjoshi4691
    @prashantjoshi4691 4 роки тому +2

    ek graphic tablet lele code likhne ke liye paint main, 8000 ke aas pass aata hai wacom bamboo ka

  • @NikhilKumar-dk5eb
    @NikhilKumar-dk5eb 2 роки тому

    thank

  • @akhileshgoswami7699
    @akhileshgoswami7699 Рік тому

    simplest solution using inorder
    void insert(Node* & root, Node * & node){
    node->right = root;
    root->left = node;
    node = root;
    }
    void inorder(Node* root, Node* &node, Node * pre){
    if(!root)return;
    inorder(root->left,node,pre);
    insert(root,node);
    inorder(root->right,node,pre);
    }

  • @hrishikeshsuryavanshi5673
    @hrishikeshsuryavanshi5673 3 роки тому

    Here you have used pass by ref ,so don't need to take f extra variable for checking, initially head is null but after that it won't

  • @sakshid1724
    @sakshid1724 2 роки тому

    This works
    class Solution
    {
    public:
    Node* head = NULL;
    Node* prev = NULL;
    void solve(Node* root){
    if(root == NULL)
    return;
    solve(root->left);
    if(prev==NULL){
    head = root;
    prev = root;
    }
    else{
    prev->right = root;
    prev->right->left = prev;
    prev = root;
    }
    solve(root->right);
    }

    //Function to convert binary tree to doubly linked list and return it.
    Node * bToDLL(Node *root)
    {
    solve(root);
    return head;
    }
    };

  • @varunbansal8865
    @varunbansal8865 2 роки тому

    bro code is not working , segmentation error aa rha he

  • @samadhanmaske5441
    @samadhanmaske5441 3 роки тому +1

    Nice explanation!
    Can someone explain or send code if we want to print ''Preorder' or 'Postorder traversal'?

    • @Abhishek-or9bd
      @Abhishek-or9bd 2 роки тому

      Preorder(Node* root){
      coutdata);
      Preorder(root->left);
      Preorder(root->right);
      }
      Postorder(Node* root){
      Postorder(root->left);
      Postorder(root->right);
      coutdata;
      }

  • @ghazanferwahab5673
    @ghazanferwahab5673 3 роки тому +1

    gfg ka set 3 ka code 100 percent accept hora hai set 2 ka error dera hai aur yeh codelibrary ka code jab java mein likha to koi output hi nai aya

  • @shubhamsood1406
    @shubhamsood1406 4 роки тому +4

    instead of prev->right->left= prev we can also write root->left= prev ??

    • @tusharshaily
      @tusharshaily 3 роки тому

      but we have to return the head in the end so , we cant update the head like that

    • @shubhamsood1406
      @shubhamsood1406 3 роки тому

      @@tusharshaily I am not talking about updating the head, I am saying that root->left= prev that is the same as prev->right->left= prev

    • @HimanshuYadav-yj4to
      @HimanshuYadav-yj4to 3 роки тому +4

      Bro I also did the same... It works fine...

    • @nikhilnaidu1383
      @nikhilnaidu1383 3 роки тому

      yes you can

  • @premanandasahoo9419
    @premanandasahoo9419 4 роки тому +1

    🔥 🔥

  • @ghazanferwahab5673
    @ghazanferwahab5673 3 роки тому

    concept samajh aagay but code ka output nai aya//c++ mein segmentation error

  • @rohansamanta7819
    @rohansamanta7819 3 роки тому

    Can someone explain the use of call by reference for "prev" ?

  • @Coding_Student144
    @Coding_Student144 4 роки тому +2

    Bro in question ko lgakar coding round clear ho jayega kyu

    • @CodeLibrary
      @CodeLibrary  4 роки тому +1

      Yaa

    • @CodeLibrary
      @CodeLibrary  4 роки тому +1

      But don't try to learn this questions... try to understand the concept behind the questions

    • @Coding_Student144
      @Coding_Student144 4 роки тому

      @@CodeLibrary yes 👍

  • @parikshitgupta7711
    @parikshitgupta7711 2 роки тому

    yaar bhai code toh description mein daal diya kar, copy paste karne mein aasame rahegi

    • @helloUser12373
      @helloUser12373 2 роки тому

      Don't copy paste, write by your own even if by seeing it.

  • @agree.
    @agree. 3 роки тому

    answer coming wrong if reference is not passed with f and also with prev, can anybody explain??

    • @aayushanand8285
      @aayushanand8285 3 роки тому

      I guess it is due to some stack issues (the stack of function calls). I guess there is some stack corruption in play here. Don't worry about that. You can easily get over this issue by making head and prev pointers global/ (or in gfg you can declare those as a member variable for the "Solution" class (public or private doesn't matter). You must although initialize them with the value "nullptr" before calling the recursive function. Also take a note that now you don't need to pass the head prev pointers by reference now. You only need to pass root and f.

  • @pankajray9533
    @pankajray9533 3 роки тому

    Please upload the video of bst

  • @sudipc9880
    @sudipc9880 3 роки тому

    Understand

  • @ujjwalmittal3785
    @ujjwalmittal3785 3 роки тому

    Expected auxillary space should be height of tree and in this problem video it would be no of nodes in tree .

  • @username_0_0
    @username_0_0 3 роки тому

    Done!

  • @ghazanferwahab5673
    @ghazanferwahab5673 3 роки тому

    c++ mein jab yeh code run kiya to segmentation error dera hai,ajeeb chutiyaap hora hai gfg mein

    • @Arya-nc4rg
      @Arya-nc4rg 3 роки тому

      same bro har question me error deta h gfg

  • @von9972
    @von9972 3 роки тому

    Bhai reading n kia kro logic intuit ni kr skte to likh kr pdha n kro

  • @prabaljainn
    @prabaljainn 3 роки тому +2

    bhai explain thoda kam kiya hai ques banaya hai aapne ,

    • @RP-qv9sc
      @RP-qv9sc 3 роки тому +3

      ekdum hi noob hai fir tu sab samajh aa gya mujhe to itna badiya explain kiya hai

  • @deepanshukumar7290
    @deepanshukumar7290 4 роки тому

    bro 2-3 din mai binary tree khtm kar du..

  • @rithikdutt1332
    @rithikdutt1332 3 роки тому

    Easy C++ solution, solution by inorder traversal
    class Solution
    {
    public:
    Node* inorder(Node *root){
    if(root->left == NULL && root->right == NULL) {
    return root;
    }
    Node *temp1 = NULL;
    if(root->left) temp1 = inorder(root->left);
    Node *temp2 = NULL;
    if(root->right) temp2 = inorder(root->right);
    root->right = temp2;
    if(temp2){
    temp2->left = root;
    }
    if(temp1){
    Node *tail = temp1;
    while(temp1->right != NULL){
    temp1 = temp1->right;
    }
    temp1->right = root;
    root->left = temp1;
    return tail;
    }
    else{
    root->left = NULL;
    return root;
    }

    }
    Node * bToDLL(Node *root)
    {
    // your code here
    root = inorder(root);
    return root;
    }
    };

  • @songs-pu9bq
    @songs-pu9bq 3 роки тому +1

    Bhai tum na hote to kya hota

  • @prabaljainn
    @prabaljainn 3 роки тому

    Tone is so boring i got some sleep