Ques: 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.  Find error in the following code: #include using namespace std;   // Tree Node struct Node {     int data;     Node* left;     Node* right; };     {     Node* temp = new Node;     temp->data = val;     temp->left = NULL;     temp->right = NULL;          return temp; }   // Function to Build Tree

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

 Ques: 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.

 Find error in the following code: #include <bits/stdc++.h>

using namespace std;

 

// Tree Node

struct Node

{

    int data;

    Node* left;

    Node* right;

};

 

 

{

    Node* temp = new Node;

    temp->data = val;

    temp->left = NULL;

    temp->right = NULL;

    

    return temp;

}

 

// Function to Build Tree

Node* buildTree(string str)

{   

    // Corner Case

    if(str.length() == 0 || str[0] == 'N')

            return NULL;

    

    // Creating vector of strings from input 

    // string after spliting by space

    vector<string> ip;

    

    istringstream iss(str);

    for(string str; iss >> str; )

        ip.push_back(str);

        

    // Create the root of the tree

    Node* root = newNode(stoi(ip[0]));

        

    // Push the root to the queue

    queue<Node*> queue;

    queue.push(root);

        

    // Starting from the second element

    int i = 1;

    while(!queue.empty() && i < ip.size()) {

            

        // Get and remove the front of the queue

        Node* currNode = queue.front();

        queue.pop();

            

        // Get the current node's value from the string

        string currVal = ip[i];

            

        // If the left child is not null

        if(currVal != "N") {

                

            // Create the left child for the current node

            currNode->left = newNode(stoi(currVal));

                

            // Push it to the queue

            queue.push(currNode->left);

        }

            

        // For the right child

        i++;

        if(i >= ip.size())

            break;

        currVal = ip[i];

            

        // If the right child is not null

        if(currVal != "N") {

                

            // Create the right child for the current node

            currNode->right = newNode(stoi(currVal));

                

            // Push it to the queue

            queue.push(currNode->right);

        }

        i++;

    }

    

    return root;

}

 

 

 

 

struct Node

{

    int data;

    struct Node* left;

    struct Node* right;

    

    Node(int x){

        data = x;

        left = right = NULL;

    }

};

 

class Solution

{

    public: 

    //Function to convert binary tree to doubly linked list and return it.

    void solve(Node* root, Node* &head, Node* &prev) {

        if(root == NULL) {

            return;

        }

        

        solve(root->left, head, prev);

        

        if(prev == NULL) head = root;

        else {

            prev->right = root;

            root->left = prev;

        }

        prev = root;

      

      

        solve(root, head, prev);

        return head;

    }

};

 

/* Function to print nodes in a given doubly linked list */

 

{

    Node *prev = NULL;

    while (node!=NULL)

    {

        cout << node->data << " ";

        

        node = node->right;

   

    while (prev!=NULL)

    {

        cout << prev->data << " ";

        prev = prev->left;

    }

    

}

 

void inorder(Node root)

{

   if (root != NULL)

   {

       inorder(root->left);

        inorder(root->right);

   }

}

 

 

main()

{

  int t;

  cin >> t;

  getchar();

  

  while (t--)

  {

     string inp;

     getline(cin, inp);

     Node *root = buildTree(inp);

     

}

Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Types of trees
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education