Flashcards
Lowest Common Ancestor
Cards in this deck (1)
- 1LCAclass Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
# Recursive
# if root.val >= min(p.val, q.val) and root.val <= max(p.val, q.val):
# return root
# node = root
# if q.val>node.val and p.val>node.val:
# return self.lowestCommonAncestor(root.right,p,q)
# elif q.val= min(p.val, q.val) and root.val <= max(p.val, q.val):
return root
elif root.val < min(p.val, q.val):
root = root.right
else:
root = root.left
Information
- Description
- Flashcards on Lowest Common Ancestor, created by Suhas S on 09/09/2019.
- Created by
- Suhas S
- Views
- 0
- Rating