Created
April 18, 2020 13:07
-
-
Save sampettersson/c7d5fa2dbf0f9c9bab639e2bd69a90a1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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