```
function invertTree(root) {
var stack = [root];
while (stack.length) {
var n = stack.pop();
if (!n) {
continue;
}
[n.left, n.right] = [n.right, n.left];
stack.push(n.left, n.right);
}
return root;
}
```