Flatten Binary Trees to Linked Lists

Flatten Binary Trees to Linked Lists

114. Flatten Binary Tree to Linked List

Turn a binary tree into a linked list in place. Use preorder traversal order.

Build from the bottom up.

class Solution {
public:
    void flatten(TreeNode* root) {
        preorder(root);
    }
private:
    void preorder(TreeNode *node) {
        if (node == NULL) return;
        preorder(node->right);
        preorder(node->left);
        node->right = pre;
        node->left = NULL;
        pre = node;
    }
    TreeNode* pre=NULL;
};

BST to Doubly Linked List

#tree #bottom-up #linked-list