Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void bToDLL(Node *root, Node **head_ref)
- {
- stack<Node*> s;
- if (root == NULL) *head_ref = NULL;
- *head_ref = NULL;
- Node* prev = NULL;
- Node* curr;
- Node* trav = root->left;
- s.push(root);
- while (!s.empty())
- {
- while (trav!=NULL)
- {
- s.push(trav);
- trav = trav->left;
- }
- curr = s.top();
- if (*head_ref == NULL)
- {
- *head_ref = curr;
- prev = curr;
- }
- else
- {
- curr->left = prev;
- prev->right = curr;
- prev = curr;
- }
- trav = (s.top())->right;
- s.pop();
- }
- Node* helper = NULL;
- if (*head_ref == NULL) cout << " " << endl << " " << endl;
- else
- {
- while ((*head_ref) != NULL)
- {
- cout << (*head_ref)->data << ' ';
- helper = (*head_ref);
- *head_ref = (*head_ref)->right;
- }
- cout << endl;
- *head_ref = helper;
- while ((*head_ref) != NULL)
- {
- cout << (*head_ref)->data << ' ';
- (*head_ref) = (*head_ref)->left;
- }
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement