Untitled
Anonymous
python
04/25/2024 9:52 PM
604 B
11
Indexable
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
def traverse(curr):
if not curr:
return None
if curr in [p, q]:
return curr
l = traverse(curr.left)
r = traverse(curr.right)
if l and r:
return curr
return l or r
return traverse(root)Editor is loading...
Leave a Comment