import Foundation enum LeetCode { static func hammingWeight(_ n: Int) -> Int { n == 0 ? 0 : (n & 1) + hammingWeight(n >> 1) } static func singleNumber(_ nums: [Int]) -> Int { nums.reduce(0, ^) } static func containsDuplicate(_ nums: [Int]) -> Bool { nums.count != Set(nums).count } static func sortedSquares(_ nums: [Int]) -> [Int] { nums.map { $0 * $0 }.sorted() } static func productExceptSelf(_ nums: [Int]) -> [Int] { let n = nums.count var result = [Int](repeating: 1, count: n) var prefix = 1 for i in 0.. TreeNode? { var invertedTree: TreeNode? if let val = root?.val { invertedTree = TreeNode(val, invertTree(root?.right), invertTree(root?.left)) } else { invertedTree = root } return invertedTree } } 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 } }