Share my code to solve Flatten Binary Tree to Linked List with iteration


  • 0
    J

    I solve this problem with no use of stack.
    Please let me know if there is any problem.

    void flatten(TreeNode *root) {
        while(root){
            TreeNode* tmp = root->right;
            root->right = root->left;
            root->left = nullptr;
            TreeNode* curr = root;
            while(curr->right){
                curr = curr->right;
            }
            curr->right = tmp;
            root = root->right;
        }	
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.