Flatten Binary Tree to Linked List

 

    TreeNode* flat(TreeNode *root)
    {
        if(!root)
            return NULL;
        TreeNode *tail1 = flat(root->left);
        TreeNode *tail2 = flat(root->right);
        if(tail1)
        {
            tail1->right = root->right;
            tail1->left = NULL;
            root->right = root->left;
            root->left = NULL;
        }
        
        if(tail2) return tail2;
        if(tail1) return tail1;
        return root;
        
    }
    void flatten(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
       flat(root);
    }

  

posted @ 2013-05-29 21:34  summer_zhou  阅读(106)  评论(0编辑  收藏  举报