/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init() { self.val = 0; self.left = nil; self.right = nil; }
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
* self.val = val
* self.left = left
* self.right = right
* }
* }
*/
class Solution {
func invertTree(_ root: TreeNode?) -> TreeNode? {
var queue = [root]
while !queue.isEmpty {
let first = queue.removeFirst()
if first == nil {
continue
}
// left, right 두개의 위치를 바꾼다
let temp = first?.left
first?.left = first?.right
first?.right = temp
queue.append(first?.left)
queue.append(first?.right)
}
return root
}
}