Binary Tree  to DLL | Practice | GeeksforGeeks

Given a Binary Tree (BT), convert it to a Doubly Linked List(DLL) In-Place. The left and right pointers in nodes are to be used as previous and next pointers respectively in converted DLL. The order of nodes in DLL must be same as Inorder of the given Binary Tree. The first node of Inorder traversal (leftmost node in BT) must be the head node of the DLL.
Example 1:
Input:
      1
    /  \
   3    2
Output:
3 1 2 
2 1 3 
Explanation: DLL would be 3<=>1<=>2

Example 2:
Input:
       10
      /   \
     20   30
   /   \
  40   60
Output:
40 20 60 10 30 
30 10 60 20 40
Explanation:  DLL would be 
40<=>20<=>60<=>10<=>30.
Your Task:
You don't have to take input. Complete the function bToDLL() that takes root node of the tree as a parameter and returns the head of DLL . The driver code prints the DLL both ways.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(H).
Note: H is the height of the tree and this space is used implicitly for the recursion stack.
Constraints:
1 ≤ Number of nodes ≤ 105
1 ≤ Data of a node ≤ 105
0
ssprateekgaur9993 days ago
class Solution{   public:       //Function to convert binary tree to doubly linked list and return it.      Node* solve(Node* root){
       
       //base cases
       if(root==NULL) return root;       if( !root->left && !root->right){           return root;       }              if(root->left==NULL){           Node* r=solve(root->right);            while(r->left){               r=r->left;           }             r->left=root;           root->right=r;           return root;       }
       if(root->right==NULL){           Node* l=solve(root->left);                      while(l->right){               l=l->right;           }           l->right=root;           root->left=l;           return root;       }       
       //Hypothesis
       Node* l=solve(root->left);       Node* r=solve(root->right);       
       //Induction       while(l->right!=NULL) {           l=l->right;       }       l->right=root;       root->left=l;              while(r->left!=NULL){           r=r->left;       }        r->left=root;       root->right=r;       return root;   }      Node * bToDLL(Node *root)   {       // your code here       solve(root);       while(root->left!=NULL){           root=root->left;       }       return root;    }};
0
rmn51245 days ago
class Solution
{
    public: 
    //Function to convert binary tree to doubly linked list and return it.
       void inorder(Node*root,Node* &head,Node* &prev,int& f){
        if(!root) return;
        inorder(root->left,head,prev,f);
        if(f==0){
            head=root;
            prev=head;
            f=1;
        }
        else{
            prev->right=root;
              prev->right->left=prev;
            prev=prev->right;
             
        }
         inorder(root->right,head,prev,f);
    }
    //Function to convert binary tree to doubly linked list and return it.
    Node * bToDLL(Node *root)
    {
        // your code here
        int f=0;
        Node*head=NULL;
        Node*prev=NULL;
        inorder(root,head,prev,f);
        return head;
    }

};
0
harshilgupta00996 days ago
Node* prev=NULL;
    Node * bToDLL(Node *root)
    {
        if(root==NULL)
            return root;
        Node* head=bToDLL(root->left);
        if(prev==NULL)
            head=root;
        else{
            root->left=prev;
            prev->right=root;
        }
        prev=root;
        bToDLL(root->right);
        return head;
    }
+2
madhukartemba2 weeks ago
JAVA SOLUTION:
class Pair
{
    Node minNode, maxNode;
    
    Pair(Node minNode, Node maxNode)
    {
        this.minNode = minNode;
        this.maxNode = maxNode;
    }
}

class Solution
{
    
    private Pair rec(Node root)
    {
        if(root==null) return null;
        
        Pair left_pair = rec(root.left);
        
        Pair right_pair = rec(root.right);
        
        if(left_pair!=null)
        {
            left_pair.maxNode.right = root;
            root.left = left_pair.maxNode;
        }
        
        if(right_pair!=null)
        {
            right_pair.minNode.left = root;
            root.right = right_pair.minNode;
        }
        
        if(left_pair==null && right_pair==null) return new Pair(root, root);
        if(left_pair!=null && right_pair==null) return new Pair(left_pair.minNode, root);
        if(left_pair==null && right_pair!=null) return new Pair(root, right_pair.maxNode);
        else return new Pair(left_pair.minNode, right_pair.maxNode);
        
        
        
    }
    
    
    //Function to convert binary tree to doubly linked list and return it.
    Node bToDLL(Node root)
    {
        Pair ans = rec(root);
        
        return ans.minNode;
    }
}
+1
harshitgarhewal4562 weeks ago
Java
 
class Solution{   Node prev = null;   Node head = null;   void inorder(Node node){       if(node == null){           return;       }       inorder(node.left);                  node.left = prev;       if(prev!=null)           prev.right = node;       prev = node;                   inorder(node.right);                       }   //Function to convert binary tree to doubly linked list and return it.   Node bToDLL(Node root)   {//  Your code her        Node curr = root;        while(curr!=null){            head = curr;            curr = curr.left;        }        inorder(root);        return head;     }       }
+2
user_990i2 weeks ago
 Node* head=NULL,*prev=NULL;   void bd(Node* root){       if(!root)return;       bd(root->left);       if(!head){           head=root;           prev=root;       }       else{           prev->right=root;           root->left=prev;       }       prev=root;       bd(root->right);          }   Node * bToDLL(Node *root)   {       head=NULL;       prev=NULL;       bd(root);       return head;          }
0
akshitt125
This comment was deleted.
0
tthakare732 weeks ago
//Java Solution 
//hint -> inorder & queue
class Solution{   
    Queue<Node> q=new LinkedList<>();
    void inOrder(Node root){
        if(root==null){
            return;
        }
         inOrder(root.left);
         q.add(root);
         inOrder(root.right);
   }
 
   Node bToDLL(Node root){
       //Variables
       Node pre = null, current = null, ResultH = null;
       
       inOrder(root);
       if(q.isEmpty()) return null;
       while(!q.isEmpty()){
          current = q.poll(); 
          if(pre == null)
              ResultH  = current; //for first Node ref to head
          else {
              pre.right = current; //a -> b
              current.left = pre;  //a <- a 
          }
          pre  = current;
       }
     return ResultH;  
   }
}
0
tthakare73
This comment was deleted.
0
ishan1010013 weeks ago
This method is giving Abort signal from abort(3) (SIGABRT) error?
 
Node * bToDLL(Node *root)   {       // your code       Node * head = NULL;       Node * tail = NULL;       stack<Node *> stk;       while(!stk.empty() || root!=NULL){           if(root!=NULL){               stk.push(root);               root = root->left;           }           else{               root = stk.top();               stk.pop();               if(head == NULL){                   head = root;                   tail = root;               }               else{                   tail->right = root;                   root->left =  tail;                   tail = root;                   root = root->right;               }           }       }       return head;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.