C++ non-recursive version


  • 0
    Y
    class Solution {
    public:
        TreeNode* upsideDownBinaryTree(TreeNode* root) {
            TreeNode* head = NULL;
            TreeNode* right = NULL;
            TreeNode* next_p = NULL;
            for (auto p = root; p != NULL; p = next_p) {
                swap(right, p->right);
                swap(p->left, p->right);
                next_p = p->right;
                p->right = head;
                head = p;
            }
            return head;
        }
    };
    

Log in to reply
 

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