Clean C++ recursive / iterative solution


  • 0

    Solution 1

    Recursive

    class Solution {
    public:
        TreeNode* invertTree(TreeNode* root) {
            if(!root) return root;
            invertTree(root->left);
            invertTree(root->right);
            swap(root->left, root->right);
            return root;
        }
    };
    

    Solution 2

    Iterative

    class Solution {
    public:
        TreeNode* invertTree(TreeNode* root) {
            stack<TreeNode*>s;
            TreeNode* p = root;
            while(!s.empty() || p){
                while(p){
                    s.push(p);
                    p = p->left;
                }
                while(!s.empty() && !p){
                    p = s.top()->right;
                    swap(s.top()->left, s.top()->right);
                    s.pop();
                }
            }
            return root;
        }
    };
    

Log in to reply
 

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