anon
2
Q:

Convert binary tree to a doubly linked list

#include <iostream> 
using namespace std; 
  
void BinaryTree2DoubleLinkedList(node *root, node **head) 
{ 
    if (root == NULL) return; 
    static node* prev = NULL; 
    BinaryTree2DoubleLinkedList(root->left, head); 
    if (prev == NULL) 
        *head = root; 
    else
    { 
        root->left = prev; 
        prev->right = root; 
    } 
    prev = root; 
    BinaryTree2DoubleLinkedList(root->right, head); 
} 
  

node* newNode(int data) 
{ 
    node* new_node = new node; 
    new_node->data = data; 
    new_node->left = new_node->right = NULL; 
    return (new_node); 
} 
  
void printList(node *node) 
{ 
    while (node!=NULL) 
    { 
        cout << node->data << " "; 
        node = node->right; 
    } 
} 
0

New to Communities?

Join the community