/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
private:
    void invert(TreeNode *node) {
        if (!node)
            return;
        stack<TreeNode*> stk;
        stk.push(node);
        while(!stk.empty()) {
            TreeNode *tmp = stk.top();
            stk.pop();
            swap(tmp->left, tmp->right);
            if(tmp->right)
                stk.push(tmp->right);
            if(tmp->left)
                stk.push(tmp->left);
        }
    }

    void invert_recursive(TreeNode *node) {
        if (!node)
            return;
        swap(node->left, node->right);
        invert_recursive(node->left);
        invert_recursive(node->right);
    }
public:
    TreeNode* invertTree(TreeNode* root) {
        invert(root);
        // invert_recursive(root);
        return root;
    }
};