【second】Flatten Binary Tree to Linked List

递归

    void flatten(TreeNode *root) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        flat(root);
    }
    
    TreeNode* flat(TreeNode* root)
    {
        if(!root)
            return NULL;
        TreeNode* left_tail = flat(root->left);
        TreeNode* right_tail = flat(root->right);
        if(left_tail)
        {
            left_tail->right = root->right;
            root->right = root->left;
            root->left = NULL;
        }
        
        if(right_tail)
            return right_tail;
        if(left_tail)
            return left_tail;
        return root;
    }

  

posted @ 2013-10-23 18:56  summer_zhou  阅读(105)  评论(0编辑  收藏  举报