LowestCommonAncestor
Published at 2021-11-21
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if p.val < root.val and q.val < root.val:
return self.lowestCommonAncestor(root.left, p, q)
elif p.val > root.val and q.val > root.val:
return self.lowestCommonAncestor(root.right, p, q)
else:
return root
# p랑 q가 둘다 현재노드보다 작다면 왼쪽탐색. 둘다 크면 오른쪽 탐색
# 위 두조건에 안걸린다면 현재 노드가 알맞다는 뜻