Flatten a binary tree into linked list | Set-2

Given a binary tree, flatten it into a linked list. After flattening, the left of each node should point to NULL and right should contain next node in level order.

Example:

Input: 
          1
        /   \
       2     5
      / \     \
     3   4     6

Output:
    1
     \
      2
       \
        3
         \
          4
           \
            5
             \
              6

Input:
        1
       / \
      3   4
         /
        2
         \
          5
Output:
     1
      \
       3
        \
         4
          \
           2
            \ 
             5

Approach: An approach using recursion has already been discussed in the previous post. A pre-order traversal of the binary tree using stack has been implied in this approach. In this traversal, every time a right child is pushed in the stack, the right child is made equal to the left child and left child is made equal to NULL. If the right child of the node becomes NULL, the stack is popped and the right child becomes the popped value from the stack. The above steps are repeated until the size of the stack is zero or root is NULL.

Below is the implementation of the above approach:

// C++ program to flatten the linked 
// list using stack | set-2 
#include <iostream>
#include <stack>
using namespace std;
  
struct Node {
    int key;
    Node *left, *right;
};
  
/* utility that allocates a new Node 
   with the given key  */
Node* newNode(int key)
{
    Node* node = new Node;
    node->key = key;
    node->left = node->right = NULL;
    return (node);
}
  
// To find the inorder traversal
void inorder(struct Node* root)
{
    // base condition
    if (root == NULL)
        return;
    inorder(root->left);
    cout << root->key << " ";
    inorder(root->right);
}
  
// Function to convert binary tree into
// linked list by altering the right node
// and making left node point to NULL
Node* solution(Node* A)
{
  
    // Declare a stack
    stack<Node*> st;
    Node* ans = A;
  
    // Iterate till the stack is not empty
    // and till root is Null
    while (A != NULL || st.size() != 0) {
  
        // Check for NULL
        if (A->right != NULL) {
            st.push(A->right);
        }
  
        // Make the Right Left and
        // left NULL
        A->right = A->left;
        A->left = NULL;
  
        // Check for NULL
        if (A->right == NULL && st.size() != 0) {
            A->right = st.top();
            st.pop();
        }
  
        // Iterate
        A = A->right;
    }
    return ans;
}
  
// Driver Code
int main()
{
    /*    1
        /   \
       2     5
      / \     \
     3   4     6 */
  
    // Build the tree
    Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(5);
    root->left->left = newNode(3);
    root->left->right = newNode(4);
    root->right->right = newNode(6);
  
    // Call the function to
    // flatten the tree
    root = solution(root);
  
    cout << "The Inorder traversal after "
            "flattening binary tree ";
  
    // call the function to print
    // inorder after flatenning
    inorder(root);
    return 0;
  
    return 0;
}

Output:

The Inorder traversal after flattening binary tree 1 2 3 4 5 6

Time Complexity: O(N)
Auxiliary Space: O(Log N)



My Personal Notes arrow_drop_up

Final year student at Indian Institute of Technology Roorkee

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.