Flatten Binary Treeto Linked List Algorithm

The Flatten Binary Tree to Linked List Algorithm is an efficient approach to converting a given binary tree into a single linked list, where all the nodes are ordered in a way that follows the tree's pre-order traversal. The algorithm modifies the tree in place, thus eliminating the need for any additional data structures or memory allocation. This transformation is particularly useful in scenarios where one needs to serialize a binary tree or perform operations that require a linear data structure instead of a hierarchical one. The algorithm starts by traversing the tree in a depth-first manner, visiting the left subtree first, followed by the right subtree. Once a node is reached, its left child is set as its right child, and its left child is set to null. The original right child is then re-attached to the rightmost leaf of the modified right subtree. This process is repeated recursively for all the nodes in the tree. As a result, the final flattened linked list will have all the tree nodes connected in their pre-order traversal sequence, with the right child pointers forming the actual links and the left child pointers set to null.
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    void flatten(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        if (root == NULL) return;
        
        stack<TreeNode*> tree_stack;
        TreeNode *flatten_node = NULL;
        tree_stack.push(root);
        
        while (!tree_stack.empty()) {
            TreeNode *node = tree_stack.top();
            tree_stack.pop();
            if (node->right) {
                tree_stack.push(node->right);
                node->right = NULL;
            }
            if (node->left) {
                tree_stack.push(node->left);
                node->left = NULL;
            }
            if (flatten_node == NULL) {
                flatten_node = node;
            } else {
                flatten_node->right = node;
                flatten_node = flatten_node->right;
            }
        }
    }
};

// recursion
class Solution {
public:
    void flatten(TreeNode *root) {
        dfs(root);
    }
    
    pair<TreeNode*, TreeNode*> dfs(TreeNode* root) {
        if (root == NULL) {
            return make_pair((TreeNode*)NULL, (TreeNode*)NULL);
        }
        pair<TreeNode*, TreeNode*> left = dfs(root->left);
        pair<TreeNode*, TreeNode*> right = dfs(root->right);
        root->left = NULL;
        root->right = NULL;
        if (left.first && right.first) {
            root->right = left.first;
            left.second->right = right.first;
            return make_pair(root, right.second);
        } else if (left.first) {
            root->right = left.first;
            return make_pair(root, left.second);
        } else if (right.first) {
            root->right = right.first;
            return make_pair(root, right.second);
        }
        return make_pair(root, root);
    }
};

LANGUAGE:

DARK MODE: