Skip to content

Commit 076a201

Browse files
committed
Kotlin: 235. Lowest Common Ancestor of a BST
1 parent f8912c5 commit 076a201

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode(var `val`: Int = 0) {
4+
* var left: TreeNode? = null
5+
* var right: TreeNode? = null
6+
* }
7+
*/
8+
9+
class Solution {
10+
fun lowestCommonAncestor(root: TreeNode?, p: TreeNode?, q: TreeNode?): TreeNode? {
11+
if (p!!.`val` > root!!.`val` && q!!.`val` > root!!.`val`) {
12+
return lowestCommonAncestor(root.right, p, q)
13+
}
14+
15+
if (p!!.`val` < root!!.`val` && q!!.`val` < root!!.`val`) {
16+
return lowestCommonAncestor(root.left, p, q)
17+
}
18+
19+
return root
20+
}
21+
}

0 commit comments

Comments
 (0)