Skip to content

Commit 6467db3

Browse files
authored
Merge pull request neetcode-gh#641 from diss1993/ticket/235
Swift | 235. Lowest Common Ancestor of a Binary Search Tree
2 parents b4a5266 + a8a497b commit 6467db3

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
func lowestCommonAncestor(_ root: TreeNode?, _ p: TreeNode?, _ q: TreeNode?) -> TreeNode? {
3+
guard let root = root, let p = p, let q = q else { return nil }
4+
if root.val < p.val && root.val < q.val {
5+
return lowestCommonAncestor(root.right, p, q)
6+
} else if root.val > p.val && root.val > q.val {
7+
return lowestCommonAncestor(root.left, p, q)
8+
} else {
9+
return root
10+
}
11+
return nil
12+
}
13+
}

0 commit comments

Comments
 (0)