Skip to content

Instantly share code, notes, and snippets.

@sampettersson
Created April 18, 2020 13:07
Show Gist options
  • Save sampettersson/c7d5fa2dbf0f9c9bab639e2bd69a90a1 to your computer and use it in GitHub Desktop.
Save sampettersson/c7d5fa2dbf0f9c9bab639e2bd69a90a1 to your computer and use it in GitHub Desktop.
/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init(_ val: Int) {
* self.val = val
* self.left = nil
* self.right = nil
* }
* }
*/
class Solution {
func invertTree(_ root: TreeNode?) -> TreeNode? {
guard let root = root else {
return nil
}
let keepLeft = root.left
let keepRight = root.right
root.left = invertTree(keepRight)
root.right = invertTree(keepLeft)
return root
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment