https://leetcode-cn.com/problems/invert-binary-tree/
//递归
func invertTree(_ root: TreeNode?) -> TreeNode? {
if root == nil {return nil}
let right = invertTree(root?.right)
let left = invertTree(root?.left)
root?.left = right
root?.right = left
return root
}
//迭代,层序遍历一次交换左右节点
func invertTree(_ root: TreeNode?) -> TreeNode? {
if root == nil {return nil}
var queue = Array<TreeNode>()
queue.append(root!)
while !queue.isEmpty {
let curNode = queue.removeFirst()
let tmpNode = curNode.left
curNode.left = curNode.right
curNode.right = tmpNode
if curNode.left != nil {queue.append(curNode.left!)}
if curNode.right != nil {queue.append(curNode.right!)}
}
return root
}