Lowest Common Ancestor of a Binary Search Tree(BST) (Leetcode 235)
Problem Link: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if(root is None):
return None
if(root.val<p.val and root.val<q.val):
return self.lowestCommonAncestor(root.right,p,q)
elif(root.val>p.val and root.val>q.val):
return self.lowestCommonAncestor(root.left,p,q)
return root
PreviousSearch in a Binary Search Tree(BST) (Leetcode 700)NextFind Bottom Left Tree Value (Leetcode 513)
Last updated