首页 > 代码库 > Flatten Binary Tree to Linked List

Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place.

For example,
Given

         1
        /        2   5
      / \        3   4   6

The flattened tree should look like:
   1
         2
             3
                 4
                     5
                         6

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


Flatten Binary Tree to Linked List