Skip to content

Commit 3fa23d0

Browse files
committed
feat: Solve lowest-common-ancestor-of-a-binary-search-tree problem
1 parent d3e9117 commit 3fa23d0

File tree

1 file changed

+14
-0
lines changed
  • lowest-common-ancestor-of-a-binary-search-tree

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
10+
if p.val < root.val and q.val < root.val:
11+
return self.lowestCommonAncestor(root.left, p, q)
12+
if root.val < p.val and root.val < q.val:
13+
return self.lowestCommonAncestor(root.right, p, q)
14+
return root

0 commit comments

Comments
 (0)