Created
April 26, 2020 15:21
-
-
Save soxjke/ec7c8170332458d8d233b8434964444a to your computer and use it in GitHub Desktop.
InvertBinaryTree(Leetcode)
This file contains 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 left = root.left | |
let right = root.right | |
root.left = invertTree(right) | |
root.right = invertTree(left) | |
return root | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment