diff --git "a/lcof/\351\235\242\350\257\225\351\242\23068 - II. \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23068 - II. \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/README.md" index 826848ea79387..68b0c1530f460 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23068 - II. \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23068 - II. \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/README.md" @@ -295,6 +295,41 @@ var lowestCommonAncestor = function (root, p, q) { }; ``` +#### Swift + +```swift +/* 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 +* } +* } +*/ + +class Solution { + func lowestCommonAncestor(_ root: TreeNode?, _ p: TreeNode, _ q: TreeNode) -> TreeNode? { + if root == nil || root === p || root === q { + return root + } + + let left = lowestCommonAncestor(root?.left, p, q) + let right = lowestCommonAncestor(root?.right, p, q) + + if let _ = left, let _ = right { + return root + } + + return left ?? right + } +} +``` + diff --git "a/lcof/\351\235\242\350\257\225\351\242\23068 - II. \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/Solution.swift" "b/lcof/\351\235\242\350\257\225\351\242\23068 - II. \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/Solution.swift" new file mode 100644 index 0000000000000..c56b48c9622ba --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23068 - II. \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/Solution.swift" @@ -0,0 +1,30 @@ +/* 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 +* } +* } +*/ + +class Solution { + func lowestCommonAncestor(_ root: TreeNode?, _ p: TreeNode, _ q: TreeNode) -> TreeNode? { + if root == nil || root === p || root === q { + return root + } + + let left = lowestCommonAncestor(root?.left, p, q) + let right = lowestCommonAncestor(root?.right, p, q) + + if let _ = left, let _ = right { + return root + } + + return left ?? right + } +}