Lowest Common Ancestor of a Binary Search Tree LeetCode solution in Python

1
class Solution:
2
def lowestCommonAncestor(
3
self, root: "TreeNode", p: "TreeNode", q: "TreeNode"
4
) -> "TreeNode":
5

6
if ((root.val >= p.val) and (root.val <= q.val)) or (
7
(root.val >= q.val) and (root.val <= p.val)
8
):
9
return root
10
elif root.val > p.val:
11
return self.lowestCommonAncestor(root.left, p, q)
12
else:
13
return self.lowestCommonAncestor(root.right, p, q)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0